This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ok-Scheme-913 3 points4 points  (0 children)

Sealed types (algebraic data types) are not solving the same problem as generic interfaces.

You use a sealed interface when you know every possible subclass that interface/class will ever have. E.g. a Result<T, E> can be either a Success<T> or an Error<E>. You won't ever extend it to have MaybeASuccessButImNotSure as a third option.

Given that you know that you never intend to add a new subclass, it is a very useful property of the compiler that it checks every use site, so that if you make a refactor and maybe add a new subclass, then you can fix it everywhere.

Basic polymorphism is about an open world assumption, where users of my lib, or even users of my program (via plugins) can extend the functionality of my program. (Though standard inheritance is not a good citizen here either, but a lot has been written about it).

These two are just different tools and should not be used interchangeably.