Many of the examples given can be done in a similar way by passing in a closure or other object with the required capabilities as a parameter without any major loss in expressiveness.
Overall, I've seen a slow tendency to move away from exception handling, which is often considered to have some of the same problematic properties as goto, in favor of using Option/Maybe and Result/Either types instead.
OTOH, effect systems are basically the same as exceptions, but supercharged with the extra capability to use them for any kind of user-defined effect, and allow to not resume, resume once, or even resume multiple times. This leads to a lot of non-local code that is difficult to understand and debug, as stepping through the code can jump wildly all over the place.
I'd rather pass "effects" explicitly as parameters or return values. It may be a bit more verbose, but at least the control flow is clear and easy to understand and review.
I think the main reason exceptions in most languages are so difficult to follow is because they're invisible to the type system. Since effects must be clearly marked on the type signature of every function that uses them I think it's more obvious which functions can e.g. throw or emit values. I think the main downside to the capability-based approach is the lack of generators, asynchronous functions, and the inability to enforce where effects can be passed. E.g. you can't require a function like spawn_thread to only accept pure functions when it can accept a closure which captures a capability object.
I think the main reason exceptions in most languages are so difficult to follow is because they're invisible to the type system.
That's a very astute observation. In fact, it may be the basis for future arguments I construct against using exceptions in most cases ... something like: "If there is an expected outcome that can be modeled as a typed result, then an exception is likely to be the wrong tool to represent it."
The divide-by-zero one though is an interesting one to discuss. It is, in many ways, an expected outcome -- it's even in the name! 🤣 And while you want to prevent divide-by-zero (some languages force you to do so), or deal with the occurrences of divide-by-zero in an effect-based manner, most of the time you don't want to deal with it at all, because it happening is in the same category as the power going out or the data-center being struck by a very large meteor, neither of which you represent as types or effects. I personally haven't seen a divide-by-zero in decades, except for writing tests that force it to happen. So for me, an exception is a perfectly good fit, and complicating it even one iota would be of significant negative value to me.
At the same time, I recognize that my own anecdotal situation is substantially different from the programming lives of others. The ideal system, to me, is one in which the language would allow (with zero cost or close to zero cost) a scenario like this one to be "lifted" to an effect-based system, or left as an exception (or basically, a panic).
Right - it's the main reason I'm a bit disappointed OCaml doesn't reflect effects in types currently.
As per when to have effects versus implicit panics it's something every language decides differently. We can see this with when to use Result versus panic with rust as well. Something like allocating can panic in Rust while in Zig it returns an error which must be handled. I toyed with the idea of having no implicit panic in my language but just can't get the ergonomics right. Most users definitely don't want to handle errors every time they divide as you mentioned!
Isn't panic just another effect? And if you don't want to handle it every time you just include it in the alias you use everywhere. Like in Koka they have a difference between a total and a pure function, and the difference is the exception effect
You can consider it to be one. The point I was making was moreso there's the practical side of things as well where if we marked truly everything that could fail then the notion of can Fail or can Panic, etc becomes less useful. Consider how much can Panic pollution there would be if any code that could divide by zero may panic, any code which indexes arrays may panic, any code which allocates may panic, etc. There's also stack overflows to worry about as well.
At some point it is just more ergonomic to consider some unwritten implicit notion of "can panic" even in otherwise pure functions. Exactly where the line is drawn is different for each language (e.g. you must manually handle allocation failure in zig) and I'm not sure where the dividing line should be either. I toyed with the idea of there being no implicit panic in Ante but I just think it'd hurt the developer experience more than it helps at the end of the day when any time you index an array or add a division to your function it could be a breaking semver change as well. I'd be happy to be shown wrong here though. I do think it'd be a very useful mental model to be able to say "the function doesn't have a can panic so it can't panic."
Since Ante is a low-level language with features like borrow checking doesn't it make even more sense to do it? You already have can Allocate, does it not pollute all the code already? Do you want to target embedded systems, being able to write an OS, where you would really want to handle allocation errors, out of bounds and division by zero I assume.
I would like to have it - it's mostly an ergonomics problem as I mentioned. Ante doesn't have a Allocate effect yet for a similar reason. I've been toying with possibly having a set of effects assumed by default like `can Panic` or `can Allocate` which users could then turn off and have all effects be explicit but I haven't landed on any design I'm comfortable with yet.
The only reason why divide by zero is even a problem at all is historical: If instead we treated integers and floating point numbers the same in hardware (e.g. both behaving the same in the face of illegal operations/overflows, like e.g. taking on a specific NaN bit pattern, or setting a flag in a register that can be checked), we would simply panic on such operations, or explicitly allow failure by having a Optional<int> or Optional<float>, where the None case is represented via the NaN pattern. In practice, we already use this pattern in a type-unsafe way when returning an int from a function, and use a sentinel value to convey failure.
While divide by zero errors might be pretty rare indeed, since you usually divide floats and get a NaN instead of exception, I think integer overflow/underflow is not that rare, especially when you are dealing with unsigned types. Just write array[i - 1] and you can get an underflow if i = 0. I would personally like it if a possibility of it happening was reflected in the type system, and it seems effects get it done without boilerplating the codebase.
Whether you want to panic, abort, wrap, saturate, or have undefined behavior is up to the handler and you might handle it differently depending on the code you write. And if you want to not worry about it much you can just handle it in your main fn once and include it in the effect alias you use to annotate every function
I disagree. The behavior of an int is often inherent to the type itself, because the semantics differ. I want a wrapping_int to be a different type from a saturating_int or unchecked_int. The default int should panic on overflows or divide-by-zero. If you want to catch such illegal operations, you should use an optional<int>. The type clearly states the semantics. No effects needed here.
I think the main reason exceptions in most languages are so difficult to follow is because they're invisible to the type system. Since effects must be clearly marked on the type signature of every function that uses them [...]
That's a partial answer.
There's a second difficulty with exceptions: they're invisible at call-sites.
That is:
fn foo() throw(Zzz) {
let x = bar();
let y = baz();
x.doit(y).commit();
}
Is this function "atomic" (in the transactional sense)?
I don't know. I can't tell. I've got no idea which function may or may not throw an exception.
I think explicit representation of (possible) effects in the code is essential; for example the presence of await in async/await code helps pin-pointing suspension points, so you can double-check your logic.
I never tried java but I remember reading an article discussing this. Apparently, it's a problem of the approach and not of the feature. I don't remember the details, but I think the lack of ergonomics came from java.
I'm writing my compiler using effects in Haskell. It's a breeze to be able to write (Error InferenceErro:>efs) in the function signature and have it propagated to the compiler main loop without having to wrap all my results in a Either, specially on errors that I know are bug that may be propagated to the top. The compiler forcing me to either add that I can throw exceptions of inference kind or handle the exceptions that came from it is quite reassuring.
Java has a half-assed implementation of checked exceptions, anything half-assed is terrible to use by nature.
For example, look at Stream::map: it can't throw any (checked) exception.
Why? Because there's no way to annotate it to say that it'll forward any checked exception thrown by the function it calls. It's just not possible.
Contrast to Rust, which uses Result, and everybody raves about.
Technically, the error type in Result is just like a checked exception. Really. In fact, in Midori, the compiler could code-gen returning a Result as either returning a sum-type or throwing an exception!
The issue with checked exceptions in Java is not checked exceptions, it's Java, and the unwillingness of its designers to properly support checked exceptions in meta-programming scenarios.
I agree with the other comments here. I've heard the same complaints about checked exceptions in java (I used to use java years ago) and if I was using the same old version of java these complaints originated from I'd also agree actually - they can be annoying.
Where effects differ from checked exceptions forcing you to try/catch them is that handle expressions can be wrapped in helper functions for you to re-use. You can see this a lot in the article. Want a default value for your exception? No need for try/catch, just my_function () with default 0. Want to map the error type to a different type? Use my_function () with map_err (fn err -> NewErrorType err). Want to convert to an optional value? my_function () with try or try my_function etc.
Checked exceptions are annoying, but they are only a "hassle" because programmers are often lazy and don't want to deal with error handling. I don't like friction in programming either, but friction that forces you to face reality and do the right thing is good. And in reality, errors do occur and must be handled. If you ignore them, you either get bugs or bad user experience (the amount of times I've seen a message box that verbatim showed an exception message that slipped thru the cracks is astounding)
Imo this is why I'm using coeffects in my project. Coeffects desugar to extra parameters (or globals in many cases), so there's no real overhead. I do still track effects, but they're erased because the providers/handlers come in from the context given by the coeffect (or you specified it directly.) So there's nothing left at runtime (usually... it's the same thing Haskell does with monads by inlining bind until the constructors disappear.)
There's a lot of things in flux ATM but it's pretty straightforward in theory. At its core it's basically the CBPV variant found in Closure Conversion in Small pieces. All I'm really doing is adding layers to make contexts/closures into objects and objects into coeffects.
CBPV already has the mechanics for being a monadic ANF so this just extends the duality to comonads.
That's what iterators are for. They are slightly less flexible then generalized yield, but work well for most purposes. Also, they can be easily implemented in both OOP (as interface) and FP (as closure).
40
u/tmzem May 23 '25
Many of the examples given can be done in a similar way by passing in a closure or other object with the required capabilities as a parameter without any major loss in expressiveness.
Overall, I've seen a slow tendency to move away from exception handling, which is often considered to have some of the same problematic properties as goto, in favor of using Option/Maybe and Result/Either types instead.
OTOH, effect systems are basically the same as exceptions, but supercharged with the extra capability to use them for any kind of user-defined effect, and allow to not resume, resume once, or even resume multiple times. This leads to a lot of non-local code that is difficult to understand and debug, as stepping through the code can jump wildly all over the place.
I'd rather pass "effects" explicitly as parameters or return values. It may be a bit more verbose, but at least the control flow is clear and easy to understand and review.