by XorNot · 1y ago · 0 comments
What I am yet to find in any programming language is a reliable way to put my cursor somewhere in the code base, and know exactly all the possible errors which can happen, where they could come from, and whether I've handled them (or might be interested in handling them).
Fault-injection is also consistently a sore point - i.e. in Go if you're doing an os.Open(somefile) call - it'd be really nice if writing code which tests the failure path was just easier - i.e. most of the time I'm saying "this Open on this line, just return an error for this test case so I can make sure it does what I want" - without bogging myself down in dependency injection or call monitoring (i.e. realistically I just want to say "os.Open in file somefile.go, line 143 - for this test please just return err"). Yes something like this can be done, but can we really not make this easier? Write tools which make this easier?
Ideally we'd have unions which gets rid of the bike shedding that comes with sums [1].
(Zig is similar but doesn't support different types of payloads on the different error values).
In your case you can either model it with a sum type or wrap each variant in its own type and take the union of them.
Example:
enum Result {
Success(String),
Error(String),
}
struct Success(String);
struct Error(String);
type Result = Success | Error;You can do this in Scala with an IO type e.g ZIO.
All functions have a return type of IO[Success, Error] and it will always return one or the other.
However, in the near future, the compiler itself should be able to handle all exceptions statically:
- https://docs.scala-lang.org/scala3/reference/experimental/ca...
- https://docs.scala-lang.org/scala3/reference/experimental/cc...
The LSP doesn't show where the exceptions are raised from, but that'd be cool. Even just having a link to pull up a search for a given exception would be handy.
Zig's error types and error unions seem like a decent approach to achieve this.