-
-
Notifications
You must be signed in to change notification settings - Fork 372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EXPERIMENTAL] Implement support for role's COMPOSE submethod in Raku v6.e #5312
base: main
Are you sure you want to change the base?
Conversation
The submethod is implementing the funcionality many expect from a phaser of the same name. But the submethod can be considered more fit to the task as it's kind of getting into the line with construction submethods `BUILD` and `TWEAK` with the difference that it is role-specific and it is about type-composition time, not object-composition. The submethod is invoked on role concretization before it gets applied to a class.
This is an experimental implementation for my proposal in Raku/problem-solving#376. With the following snippet the submethods would be invoked for R0, R1, R2 in this order: use v6.e.PREVIEW;
role R0 {
submethod COMPOSE {
note "COMPOSE on ", self.^name, " of ", self.HOW.^name, " for ", ::?CLASS.^name;
}
}
role R1 does R0 {
submethod COMPOSE {
note "COMPOSE on ", self.^name;
}
}
role R2 does R0 {
submethod COMPOSE() {
note "COMPOSE on ", self.^name, " // ", self.HOW.^name, " // ", self ~~ R2;
}
}
class C does R1 does R2 { } |
As a matter of fact, it would better do as a sub, but we don't use global subs in the Metamodel. Therefore, for now, I move it into Perl6::Metamodel::Configuration. This is a temporary experimental solution as it would make more sense to have a class dedicated to utility methods of similar kind.
Make EnumHOW aware of role concretizations. The rest is done by RoleToClassApplier.
Will proto methods have their candidates available at compose time? I will grab it and check, but if you happen to test this and find out, please let me know. Here is the code I am using for testing: use Method::Also;
class A { method a is also<b> { 1 }; };
my $a = A.new;
$a.b.say;
role R {
proto method c_d (|) is also<d-e> { * };
multi method c_d { 43 };
multi method c_d (Str $a) { "a" };
};
$a does R;
$a does AliasesFromRole[R];
$a.c_d.say;
$a.d-e.say; Please note, I am working from my copy of Method-Also at https://github.com/Xliff/Method-Also, branch |
So... after a quick compile and a small change, here is my revised test code: use v6.e.PREVIEW;
use Method::Also;
class A {
method a is also<b> { 1 };
};
my $a = A.new;
$a.b.say;
role R {
also does AliasedMethods;
proto method c_d (|) is also<d-e> { * };
multi method c_d { 43 };
multi method c_d (Str $a) { "a" };
};
$a.c_d.say;
$a.d-e.say; Results:
So it appears that the COMPOSE submethod isn't late enough for the existing aliasing mechanism. Could you give a look at the existing code in Method::Also and create an issue with your recommended changes? Thanks. |
@Xliff unfortunately, I wouldn't be able to help with First of all, speaking of the Next, you operate on the class. This is a mistake. Role HOWs are consumers of |
Can a role override EDITED: Answer - No. |
Continuing our IRC conversation, a role – can't, unless it is a role mixed into HOW. See below: role AliasHOW {
has %!aliases;
method register-alias(\obj, $from, $to) {
%!aliases{$from} = $to;
}
method add_multi_method(Mu \obj, $name, \code-obj) is raw {
note "ADDING MULTI ", $name, " ", code-obj.raku;
with %!aliases{$name} {
note " $name -> $_";
callwith(obj, $_, code-obj);
}
callsame
}
}
multi sub trait_mod:<is>(Method:D \m, Str :$alias!) {
note "Alias $alias for ", m.raku;
m.package.HOW does AliasHOW unless m.package ~~ AliasHOW;
m.package.^register-alias(m.name, $alias);
}
role R {
proto method foo(|) is alias<bar> {*}
multi method foo {
}
}
class C does R { }
C.bar; And don't forget that this snippet only makes sense for the trait applied to a |
The submethod is implementing the funcionality many expect from a phaser of the same name. But the submethod can be considered more fit to the task as it's kind of getting into the line with construction submethods
BUILD
andTWEAK
with the difference that it is role-specific and it is about type-composition time, not object-composition.The submethod is invoked on role concretization before it gets applied to a class.
This is currently just an experiment. A couple of small touches would be needed to have it merge-ready.