Re:Age discrimination in Perl 6 using subsets and multiple d...
by ekiru on Sunday August 22, 2010 @03:04AM
(#1624339)
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.
Re:Age discrimination in Perl 6 using subsets and multiple d...
by mahmud on Sunday August 22, 2010 @03:18AM
(#1624351)
Subset 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.
Re:Age discrimination in Perl 6 using subsets and multiple d...
by Hexstream on Sunday August 22, 2010 @01:38PM
(#1624920)
1. The values of :after methods are ignored.
2. You're calling person-age from within the method so you enter infinite recursion.
What you were looking for is:
(defmethod person-age :around ((someone person))
(let* ((age (call-next-method))
(category (if (< age 16)
:child
(if (>= age 66)
:senior
:adult))))
(values age category)))
Re:Age discrimination in Perl 6 using subsets and multiple d...
by mahmud on Sunday August 22, 2010 @01:42PM
(#1624931)
yeah, I am on a pristine box without slime; and those written after a few beers late at night.
Re:Age discrimination in Perl 6 using subsets and multiple d...
by lispm on Sunday August 22, 2010 @03:30AM
(#1624358)
Predicate dispatch in CLOS:
ftp://publications.ai.mit.edu/ai-publications/2001/AITR-2001-006.pdf
Re:Age discrimination in Perl 6 using subsets and multiple d...
by mahmud on Sunday August 22, 2010 @03:42AM
(#1624365)
Well, there goes Sunday. I was supposed to port a bunch of stuff to ABCL tomorrow, but I can't wait to fall asleep tonight and read that paper first thing in the morning. Wish I could fork multiple selves of me to devour all these goodies, Rainer :-)
(mapcar (lambda (task)
(bt:make-thread
(lambda ()
(funcall (car task) (cdr task))))
(loop for me from 1 to *infinity*
and problem in *all-problems*
collecting (list me problem)))
Similary, print-name shouldn't exist as a standalone function for something so mundane. You should just augment the builtin PRINT-OBJECT method that specializes on the PERSON class to print suitable titles. You don't even have to instrument the main PRINT-OBJECT, just the one that prints the actual receipt to the paper-printing HARDCOPY stream.
I mean, seriously; I hope we're not blogging back and forth about CLOS-101 applications of methods.