Better HN
topnewbestaskshowjobs
Log inSign up

rto_victim

5 karma · joined Dec 6, 2024 (1y ago) · 2 submissions

Submissions

comment 1y ago · link · parent

That's correct.

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;

comment 1y ago · link · parent

Agreed. Sum types require too much boilerplate for error handling as they require wrapping and unwrapping which leads to people creating crate-level error enums that have every possible injection.

Ideally we'd have unions which gets rid of the bike shedding that comes with sums [1].

[1]: https://gvolpe.com/blog/error-handling-scala3/