8 ms·
I know that this is a pretty tame example of subset types. But do you really think that modifying the return values of person-age for everything in the system i
by ekiru 16y ago
I know that this is a pretty tame example of subset types. But do you really think that modifying the return values of person-age for everything in the system is more easily extensible than a subset type which is external to the objects? Suppose that somewhere else in the system, you want to dispatch based on whether a Person is old enough to legally drink alcohol. With subset types, you don't have to be aware of the Child/Adult/Senior partition at all. With the :after approach, you're dependent on the ordering of the after methods and you have to know about the existence of the :child/:senior/:adult test.
Here's the code for buy-beer in Perl 6:
subset CanDrink of Person where *.age >= 21;
subset CannotDrink of Person where *.age < 21;
multi buy-beer (CanDrink $person) { give $person, Beer.new }
multi buy-beer (CannotDrink $kid) { call-cops-on $kid }
No other code in the system need be aware of it. No other code in the system is affected by it.
- mahmud 16y agoSubset types, at least in that form you have shown, do not exist in Common Lisp. That's a very MLish feature, and Perl keeps on surprising me :-) Do another, more elaborate one when you have time. Would love to learn new things.