use Protocol;
protocol FormattableKeys {
method keys { ... }
method formatted-keys { $.keys.join: ", " }
}
say FormattableKeys[];
say FormattableKeys[ProtocolClass];
say FormattableKeys[ProtocolSubset];
say FormattableKeys[ProtocolCoerce];
my FormattableKeys[ProtocolCoerce] $a = %( a => 1, b => 2 );
say $a;
say $a.formatted-keys;
multi sub a(FormattableKeys[ProtocolCoerce] $a) {
say $a + 1;
say $a.formatted-keys;
say $a.^name
}
a %(x => 1, y => 2, z => 3);
my package EXPORTHOW {
package DECLARE {
use MetamodelX::Protocol;
constant protocol = MetamodelX::Protocol;
}
}
sub EXPORT(--> Map()) {
use Protocol::Type;
Protocol::Type::
}
use Protocol::Type;
unit class MetamodelX::Protocol is Metamodel::ClassHOW;
has @.yada;
role ParentRole[$parent] {
::?CLASS.^add_parent: $parent
}
role Protocolable {
multi method COERCE(Any \value where { .^methods>>.name.any ~~ $.HOW.yada.all }) {
my $parent = self.WHAT;
my \child = value but ParentRole[$parent];
child
}
}
method new_type(:$name = "<anon>") {
my $target_type := callsame;
$target_type.^add_role: Protocolable;
$target_type
}
multi method parameterize($type, ProtocolClass) {
$type
}
multi method parameterize($type, ProtocolSubset) {
my @yada = @!yada;
my subset ProtocolSubset of Any where { .^methods>>.name.any ~~ @yada.all };
}
multi method parameterize($type, ProtocolCoerce) {
Metamodel::CoercionHOW.new_type: $type.WHAT, $type.^parameterize: ProtocolSubset
}
multi method add_method(Mu, $name, & where { .yada }) {
@!yada.push: $name
}
multi method add_method(|c) { nextsame }
enum Protocol::Type <
ProtocolClass
ProtocolCoerce
ProtocolSubset
>;