Constrain return type to be type captured by role parameter

104 Views Asked by At

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?

1

There are 1 best solutions below

0
Elizabeth Mattijsen On BEST ANSWER

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")