3 ms·
In Scala, partial function is a subtype of function, even though it would seem like the opposite should be true, conceptually.
by acjohnson55 9y ago
In Scala, partial function is a subtype of function, even though it would seem like the opposite should be true, conceptually.
- kccqzy 9y agoThanks for the example. Yeah this is another case where people have trouble understanding subtyping relation and which type should be the subtype of which. And I agree with you that the opposite should be true conceptually, because if you ask yourself, "if a partial function is being demanded, can you use a function instead?" you might answer yes. Alternatively, think of a partial function conceptually as a function that returns an option type, i.e. in Haskell syntax type Function a b = a -> b type PartialFunction a b = a -> Maybe b and with the obvious addition that "b" is a subtype of "Maybe b", then we again conclude that function should be a subtype of partial function.
- acjohnson55 9y agoFor sure. And since all functions can throw exceptions, a partial function is just a function that just happens to throw a lot of them :)