r/ProgrammingLanguages • • Feb 23 '26

Error handling for functions with side effects

I've been thinking a lot about error handling since im designing my own language.

I quite like the errors as values approach where a function returns the succesful value or some error value. I find this very intuitive and it seems like a big improvement over exceptions

However, sometimes the point of a function is mainly to produce a side effect like writing to a file or setting an element of a data structure. You do not really care about the result here which i think clashes a bit with the errors as value approach. As far as i know some languages would represent by having the return be a possible error. In a language like go this can simply be ignored. Rust gives a warning if you dont handle such a result and in zig you must capture the result of a function if it returns something. I do not really like these approaches and i think having such a function throw an error makes more sense but i dont really like how this is done on most exception based languages. I quite like the approach that swift takes where it has exceptions with syntactic sugar that you would often find in errors as values based languages.

Im curious to hear how other people feel about error handling for functions that mostly about their side effects so please let me know your thoughts!

13 Upvotes

29 comments sorted by

View all comments

Show parent comment

1

u/Savings_Garlic5498 Feb 23 '26

Ive already worked through that book and have written multiple compilers already. Its not about the target, its about the design of the language.

1

u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Feb 25 '26

Excellent! That's a great start.

My advice on design is to do as much of it up front as you can, but not the rest of it. That's a simple way to say: The things you're sure of, lock down. The things you're not sure of, allow yourself to discover.