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

Roles as anonymous and/or closures

Thread Next
From:
Aaron Sherman
Date:
April 26, 2005 06:25
Subject:
Roles as anonymous and/or closures
Message ID:
1114521945.9630.104.camel@pps
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 <ajs@ajs.com>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About