r/ProgrammingLanguages • u/yassinebenaid • Jan 14 '26
Requesting criticism Panic free language
I am building a new language. And trying to make it crash free or panic free. So basically your program must never panic or crash, either explicitly or implicitly. Errors are values, and zero-values are the default.
In worst case scenario you can simply print something and exit.
So may question is what would be better than the following:
A function has a return type, if you didn't return anyting. The zero value of that type is returned automatically.
A variable can be of type function, say a closure. But calling it before initialization will act like an empty function.
let x: () => string;
x() // retruns zero value of the return type, in this case it's "".
Reading an outbound index from an array results in the zero value.
Division by zero results in 0.
View all comments
Show parent comment
2
u/pixilcode Jan 19 '26
Also u/syklemil
"Division by 0 results in 0" is mathematically sound, and it's a choice that some languages like Pony make (see this).
The explanation is something to the effect that in a field, the multiplicative inverse of
a,a-, is defined such thata * a- = 1. This is defined for every number except 0, since there is no value0-such that0 * 0- = 1.If we define division as multiplication by the inverse
a / b = a * b-, then this defines multiplication for all values ofaand all values ofbexcept0, since0-doesn't exist. This then means that proofs about division (such asa/a = 1orx/y = m -> y*m = xmust be special-cased to exclude zero in the denominator position. We can therefore define division by zero however we want without breaking those proofs. We also must note that those proofs do not apply if 0 is in the denominator position.Thus, we can define division by zero as:
x / 0.0 = NaN or Inf or -Inf.This post explains it much better than I did (I was mostly just summarizing some of the points) and goes into much more mathematical detail about it, I'd highly recommend giving it a read.
Also note his disclaimers, though. Especially "'good for a theorem prover' is not the same as 'good for a programming language'".
Also, see this StackOverflow question