problem-solving icon indicating copy to clipboard operation
problem-solving copied to clipboard

Coercer methods for parametrised roles

Open gfldex opened this issue 4 years ago • 1 comments

Currently there is no declarative way to define coercers for parameterised roles.

role R[::T] {}

class A {
    method m(R[Int]() $_) { say $_ ~~ R[Int] }
}

class B {
    method R[Int]() {}
}

# OUTPUT: Missing block
          at /home/dex/projects/raku/tmp/typed-supply.raku:35
          ------>     method R⏏[Int]() {}

The problem gets worse because there is a workaround via the MOP.

B.^add_method('R[Int]', method {
    class :: does R[Int] {
    }.new
});
B.^compose;

There is no reasonable way to stop a user from taking this workaround. Further, having the ability to provide such coercers would improve composebility. Since dispatch already works this might even be a low hanging fruit. COERCE has to be considered too.

gfldex avatar Apr 18 '21 10:04 gfldex

As b2gills pointed out, one can use method ::("R[Int]")() {} to get the same result without the MOP. This is specced in S02-names/indirect.t and an ENODOC. At least to my eyes this doesn't look right.

gfldex avatar Apr 18 '21 21:04 gfldex