Are algebraic datatypes (sealed classes) good for api design when you cant tell of all the possible variants just by looking at the funcion? without looking at the type impl details
k
karelpeeters
07/01/2018, 8:30 AM
I don't really see a problem here: they're just like normal
open
classes with lots of possible types, just a bit restricted.
u
ursus
07/01/2018, 1:24 PM
Yes but you as a consumer dont know about, when compared to maybe monads you see Either you know what to do
k
karelpeeters
07/01/2018, 1:42 PM
Well you can always look at the docs or the source, right?
u
ursus
07/01/2018, 3:03 PM
Well sure, same as you can use ints for enums .. im just asking questions, not arguing
k
karelpeeters
07/01/2018, 3:05 PM
Haha sure, I just don't really get the problem. Of course you need to look at the docs if you want to know about a certain class.
g
gildor
07/01/2018, 3:32 PM
I think sealed classes is good option for such api. If you use when as expression compiler will notify you about forgotten cases and even if new class of sealed hierarchy was added later
u
ursus
07/01/2018, 3:56 PM
@karelpeeters the problem is imo discoverability, if every sealed class is unique and from call site you dont know its a hierarchy, then intuitively you dont know what to do, but guess when I think about it, enums have the same issue