Search code examples
rolesrakuparameterized-types

Constrain return type to be type captured by role parameter


Should the following work in Raku:

my role R[::T] {
    has T $!a;
    method m ( T $v ) {
        $!a = $v;
    }
    method n ( --> T ) {
        $!a;
    }
}

my class C does R[Str] { }

my C $c = C.new;
$c.m: 'hello';
$c.n;

Running the above gives the error:

Type check failed for return value; expected T but got Str ("hello")

Am I misunderstanding how parameterized roles work?


Solution

  • This was actually more a case of Not Yet Implemented.

    It has been recently implemented by Vadim Belman and will be in the next Rakudo compiler release (see https://rakudoweekly.blog/2022/08/29/2022-35-reworkout/ "Core Developments")