develooper Front page | perl.perl6.language | Postings from April 2005

Roles as anonymous and/or closures

Thread Next
Aaron Sherman
April 26, 2005 06:25
Roles as anonymous and/or closures
Message ID:
Is any of this legal?

        class car {
        	method make ($self: $make, *@models) returns(car) {
        		return $self but role {
        			has $.make = $make;
        			method model($self: $model) returns(car) {
        				unless $model eq any(@models) {
        					die "Invalid model: $model";
        				return $self but role {
        					has $.model = $model;
        my car $generic .= new;
        my car $ford = $generic.make("ford",<pinto mustang>);
        my car $pinto = $ford.model("pinto");

It certainly seems very attractive, but I'm not sure if roles can be
anonymous and/or closures.

If we can do this, then it brings up the possibility of currying
mixin-generator methods and all sorts of other useful features which
would greatly simplify some very complex classes.

It also might be useful for roles to be able to delete members and
methods from a class like so:

	role foo {
		has $.x;
		has not $.y;

This allows a role to "reset" the application of subordinate roles (as
the example method, make, might want to reset the application of its own
"model" method's anon role).

"has not" precludes ever having a type named "not", and if that's a
problem it could read "not has" or "!has", but that feels a bit klunkier
to me.

Aaron Sherman <>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About