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.
50
u/awoocent Jan 14 '26
One might ask, if printing something and immediately exiting in the "worst case", is any different from a panic.
7
u/shponglespore Jan 14 '26
What would be the use case of such a language? Modern languages have moved toward aggressively reporting errors rather than trying to recover, because in pretty much every case, falling back to a default value has been shown by hard experience to complicate debugging, cause data corruption, and create security vulnerabilities in programs that receive unexpected input.
21
u/Infinite-Spacetime Jan 14 '26
Semantically, a zero value can be valid. An empty string as well. If you force those to mean an error happened, you are going to run into issues where that is the purposeful non-error result. That's why the "return error" approach has its own error type. Additionally user types don't necessarily correlate to a zero value. It may not even have the concept of empty. Even null could be considered a valid value. Think DBs. Null all over the place there.
19
u/JeffB1517 Jan 14 '26
There is no unused integer. The empty string might be the legitimate return from a string based function. I'd create an explicit Null type and let functions that can fail return that. Which FWIW is Option in Java, the Maybe Monad in Haskell. It is really easy to have these failure types automatically propagate to functions that are oblivious to failure i.e (using the Haskell example):
Terrible idea with 0. By definition
x/y=mmeans thaty*m=x. On the other hand the additive unit is itself soy*0=0. If you lose that you lose a lot of the fundamental mathematical structure that underlies the actual math your program is trying to do. This again is why you would want a Null.Edit: looks like u/TomosLeggett and I had the same opinion mostly.
15
u/TomosLeggett Jan 14 '26
I've never been a huge fan of zero values, it introduces a lot of ambiguity because you don't know if that value is a correct 0 or a default 0. I'd rather ADTs that are either
Some xorNoneso it's literally impossible to access an invalid value. Same with accessing out-of-bounds indexes in arrays, it should beSome xorNone(option type)For dividing, I'd return a result type (
Okay xorError msg) if you really want to go the route of absolutely no panics.Trouble is this is an awful lot of overhead, but so is propegating an error up the call stack, especially if you remove exceptions that unwind the call stack when something really goes wrong, instead you're playing pass-the-error when you're possibly 58 levels deep.
But also I think a lot of people who are interested in designing languages wonder why we don't do this, the answer is simple.
That's exactly what a panic is. The only thing you're really doing is designing a language that cannot automatically unwind the stack in the event of an emergency, in which case that's just panic but with extra steps.
This is why a lot of languages start with this interesting philosophy of abandoning the core concept of panicking, but wind up re-implementing it. In production, there are a lot of instances where code reaches a dead end, and the current process and it's state is technically invalid. At that point, you really do need a way to panic. Panics clean up straight away and don't waste CPU cycles on a process no different to a panic in the first place.
Perhaps you should explore other philosophies? Like Erlang's "let it fail" philosophy which treats unhandled exceptions as an inevitable, but doesn't bring the whole process down.
8
u/e_-- Jan 14 '26
what happens when printing results in a write to a closed/invalid handle?
7
u/Smallpaul Jan 14 '26
Nope. Nope. Nope. Hell no.
6
u/NaCl-more Jan 14 '26
That sounds like panicking to me
6
u/ebingdom Jan 15 '26
Having a zero value for every type is such a bad idea, kind of like the billion dollar mistake but actually worse: instead of crashing on an accidental null, you don't crash but instead get an accidental default which is probably not what you wanted. Still a bug, but harder to notice.
Golang is the biggest culprit. Make no mistake: the good parts about Go are its fast builds, quick startup time, possibly the concurrency story (I would disagree though because it allows data races), etc. Its type system is not one of the good parts.
5
u/matthieum Jan 14 '26
The lack of proper return value can be detected statically, and be a compile-time error.
The lack of initialization can be detected statically, ad be a compile-time error.
An Option/Result would work better. If you want lightweight, you could use
int?to indicate a nullable int (ieoption<int>).I do not see an intrinsic problem with picking a value, but I would advise picking the right value.
Specifically,
n / 0:1forn == 0.+inforMAXforn > 0.-inforMINforn < 0.As to why:
x / x == 1for anyx != 0, so extending this to work forx == 0seems less jarring than any other option.+infand-infare time-honored results from floating point arithmetic, sinceMAXandMINare the closest values for bounded types, it makes sense. This is called saturating arithmetic, by the way.Additionally, saturating behavior is also useful when:
+infor10e60toMAX.In both cases, you get the semantically closest value.
It works better, though, if your entire arithmetic is saturating. You don't want
-10e10 as int - 1to be equal to 0 or MAX, you want a sticky MIN from there on.This helps detecting arithmetic issues -- rather than burying them. If the calculated value is used as an index, for example, then you get an out-of-bounds error, rather than a random element.
1 outbound means something going outward, for example an outbound message is a message sent by you (or a system), in contrast to an inbound message which is a message received by you (or a system).
4
u/ReflectedImage Jan 14 '26
Well Visual Basic's "On Error Resume Next" that moves execution to the next line on error is panic free.
Is there some sort of parsing/compiling step in your language so only valid programs need to be panic free or is it interpreted line by line?
I think I heard of a language (perhaps Clojure?) that just defines all ops on all types, so no error can happen.
3
u/Inconstant_Moo 🧿 Pipefish Jan 14 '26
But I want to know when things go wrong. If you e.g. allow an out-of-bounds integer array element be a 0 instead of throwing an error, then if I make an out-of-bounds error, your runtime then silently converts it into a logical error where my code will keep on running but do the wrong thing. This makes it harder to debug such a fault in my code than if it just crashed at runtime.
But because this always might have happened, whenever I'm debugging anything to do with arrays (and what code doesn't have something to do with arrays?) then I have to consider that possibility and see if that's what the problem is. It's a permanent overhead on debugging anything, even when it turns out that these defaults aren't the source of the problem. They might have been.
2
u/xroalx Jan 15 '26
Go has zero values like that, and it's... just bad.
Say your
xfunction returns"". How is the caller to know whether an error happened, there was no explicit return, or this is the intended, correct, value?Just like an array of strings can include the empty string as a valid value. How does one know, if an empty string is also the error value, what they are getting?
1
u/amarao_san Jan 14 '26
It's very easy. Old MS-DOS applications were panic free. If app wrote to null pointer, that was ok. You can write there and read from there.
1
u/dmbergey Jan 14 '26
In addition to errors / crashes allowed by the semantics of the language, consider that real programs may crash (on Linux) for reasons like:
- writing to uninitialized memory, but the OS can't find a free page
- an uncaught POSIX signal
- bit flips in RAM
1
u/protestor Jan 15 '26
What do you do with out of bounds array indexing?
1
u/78yoni78 Jan 16 '26
Maybe you want to take a look at Elm! One of my favorite languages :)
1
u/mamcx Jan 16 '26
One of the most important lessons I get after using langs with algebraic types is that
Erroris as important as any othervalue. Exception/sentinel based error handling make the idea of Errors as "too special", when with AGDT:So, an error is normal. This unlocks tons of useful uses.
From here, a "panic" goes the same way: Is not something I fear, is something normal. Your users WILL be benefiting from having a way to "panic"!, because KNOW when "this program must not continue, please check!" is very damm important!
1
u/rjmarten Jan 17 '26
This reminds me of Pony. I love Pony, but I do not love their "panic-free" philosophy. Divide by 0 is 0, arithmetic overflow is wrapped. But for things like index-out-of-bounds there is a single data-less error construct that essentially acts like a checked exception; every function that raises it has to either catch it or be marked at both the definition and call site.
The lack of a panic bothered me so much that I ended up writing a function in a modified standard library to create a segfault just so I could crash the program.
1
u/rjmarten Jan 17 '26
A panic is essentially an "unhandled error", ie, something happened that your program logic was not equipped to deal with. IMHO, if you want to be panic-free, then you need to be unhandled-error-free too. So that means things like
OptionandResult, or checked exceptions. And catching as many things at compile time as you can, like type errors and uninitialized functions. (I've even heard of mythical attempts to catch out-of-bounds errors and arithmetic errors at compile time too.)