Fair warning, I'm new to functional programming so I may hold many bad assumptions.
I've been learning about algebraic types. Many functional languages seem to have them, and they are fairly useful in conjunction with pattern matching. However, what problem do they actually solve? I can implement a seemingly (sort-of) algebraic type in C# like this:
public abstract class Option { }
public class None : Option { }
public class Some<T> : Option
{
public T Value { get; set; }
}
var result = GetSomeValue();
if(result is None)
{
}
else
{
}
But I think most would agree this is a bastardization of object oriented programming, and you shouldn't ever do it. So does functional programming just add a cleaner syntax that makes this style of programming seem less gross? What else am I missing?
class ThirdOption : Option{}
and give you anew ThirdOption()
where you expectedSome
orNone
? – amon Jun 21 '15 at 18:09data Maybe a = Just a | Nothing
(equivalent todata Option a = Some a | None
in your example): you can't add a third case post-hoc. While you can sort of emulate sum types in C# in the way you've shown, it's not the prettiest. – Martijn Jun 22 '15 at 9:47