by ufo · 12y ago · 0 comments
case mx of Just x -> f(x)
vs if (x != null){ f(x) }
What you can do in a Javascript-like language is use union and intersection types. However, they can get a bit complicated (specially if you allow unions of non-primitive types) and the extra flexibility can confuse the type inference a bit so I can understand them restricting things to the common case of handling null. function nothing() {
return {
match: function(cases) { return cases.nothing(); }
};
}
function just(x) {
return {
match: function(cases) { return cases.just(x); }
};
}
just(1).match({
just: function(x) { return x + 1; }
nothing: function() { return 0; }
});