Dave Storrs <[EMAIL PROTECTED]> writes: > On Wed, Dec 18, 2002 at 09:31:41AM +0000, Piers Cawley wrote: >> Dave Storrs <[EMAIL PROTECTED]> writes: >> > It seems like Perl6 is moving farther and farther away from Perl5's >> > (almost) typelessness. >> >> It depends what you mean by typed. Perl has always had strongly typed >> *values* (which strike me as being a damn sight more useful than >> typed variables). > > No argument from me. When I want/need the abilities that come with > specifying type, I want the language to support it. I just don't want > to _have_ to do it, when I don't want/need those abilities. > > >>In a language with typed values, being able to >> declare a typed variable is useful for a few reasons: >> >> * After watching things in a profiler, you sacrifice programmer >> flexibility by typing a couple of variables as a way of giving the >> Optimizer something to get its teeth into (if you have a typed >> variable then you can limit the amount of runtime checking you have >> to do in favour of compile time checks) > > Agreed. > > >> * For setting up multiply dispatched methods and functions. Consider >> the example below (which I know I've used before). >> >> sub grep ( (Rule | Block) $selector, @*args ) { @args.grep($selector) } >> sub grep ( (Rule | Block ) $selector, Collection $collection ) { >> $collection.grep($selector) >> } >> >> sub grep ( WeirdSelector $selector, @*args ) { >> grep $selector.as_block, *@args; >> } >> >> Because we can declare the types of the function parameters we can >> let the language sort out the dispatch for us. Without typed >> parameters and multi dispatch those three function definitions >> become: >> >> sub grep ( $selector, $first, @*args ) { >> if @args.length { >> return [ $first, @args ].grep($selector); >> } >> else { >> $first.grep($selector); >> } >> } >> >> method Object::grep ($self: $selector { >> [ $self ].grep($selector); >> } > > Hm. I'm way short on sleep today, so I'm probably missing something, > but I don't see why Perl can't sort this out without a specific > typing.
Well, you've got to specify the types *somewhere* when you set up your multimethods. The parameter list seems a better place than most. > On a more nit-picky level, the first two subs in the top block seem to > show that arrays are not derived from Collection. Surely, if > everything is an object, they should be? They are, but that C<< @*ary >> is *not* the same as C<< @ary >> in an arg list, it flattens the remaining arguments into an array, on which we then call the grep method. Essentially the variant is only there to normalize the list of arguments.