Skip to content

Commit 1aff9bc

Browse files
committed
Basic MOP-level support for role arguments.
There's no syntactic sugar for it, but this allows provision of role arguments in order to do a mix-in of the parameterized role.
1 parent 41e7b93 commit 1aff9bc

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/how/NQPCurriedRoleHOW.pm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
knowhow NQPCurriedRoleHOW {
2+
has $!curried_role;
3+
has @!pos_args;
4+
5+
my $archetypes := Archetypes.new( :nominal(1), :composable(1), :parametric(1) );
6+
method archetypes() {
7+
$archetypes
8+
}
9+
10+
method new(:$curried_role!, :@pos_args!) {
11+
my $obj := nqp::create(self);
12+
$obj.BUILD(:$curried_role, :@pos_args);
13+
$obj
14+
}
15+
16+
method BUILD(:$curried_role!, :@pos_args!) {
17+
$!curried_role := $curried_role;
18+
@!pos_args := @pos_args;
19+
}
20+
21+
method new_type($curried_role!, *@pos_args) {
22+
my $meta := self.new(:curried_role($curried_role), :pos_args(@pos_args));
23+
pir::repr_type_object_for__PPS($meta, 'Uninstantiable');
24+
}
25+
26+
method specialize($obj, $class_arg) {
27+
$!curried_role.HOW.specialize($!curried_role, $class_arg, |@!pos_args);
28+
}
29+
30+
method name($obj) {
31+
$!curried_role.HOW.name($!curried_role)
32+
}
33+
34+
method curried_role($obj) {
35+
$!curried_role
36+
}
37+
}

src/how/NQPParametricRoleHOW.pm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,18 @@ knowhow NQPParametricRoleHOW {
108108
method parametric($obj) {
109109
1
110110
}
111+
112+
# Curries this parametric role with arguments.
113+
method curry($obj, *@args) {
114+
NQPCurriedRoleHOW.new_type($obj, |@args)
115+
}
111116

112117
# This specializes the role for the given class and builds a concrete
113118
# role.
114-
method specialize($obj, $class_arg) {
119+
method specialize($obj, $class_arg, *@args) {
115120
# Run the body block to capture the arguments into the correct
116121
# type argument context.
117-
$!body_block($class_arg);
122+
$!body_block($class_arg, |@args);
118123

119124
# Construct a new concrete role.
120125
my $irole := NQPConcreteRoleHOW.new_type(:name($!name), :instance_of($obj));

tools/build/Makefile.in

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ NQP_SOURCES = \
179179

180180
NQP_MO_SOURCES = src/how/Archetypes.pm src/how/RoleToRoleApplier.pm \
181181
src/how/NQPConcreteRoleHOW.pm src/how/RoleToClassApplier.pm \
182-
src/how/NQPParametricRoleHOW.pm src/how/NQPClassHOW.pm \
183-
src/how/NQPNativeHOW.pm src/how/NQPAttribute.pm \
184-
src/how/NQPModuleHOW.pm src/how/EXPORTHOW.pm \
182+
src/how/NQPCurriedRoleHOW.pm src/how/NQPParametricRoleHOW.pm \
183+
src/how/NQPClassHOW.pm src/how/NQPNativeHOW.pm \
184+
src/how/NQPAttribute.pm src/how/NQPModuleHOW.pm \
185+
src/how/EXPORTHOW.pm \
185186

186187
NQP_MO_PBC = nqpmo.pbc
187188
NQP_MO_PIR = gen/nqp-mo.pir

0 commit comments

Comments
 (0)