Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one set the default parameterization of a class?

Tags:

raku

The ^parameterize method allows one to parameterize a class with some given information, such as a role.

my role A[::T = Mu] { }

my class B {
    method ^parameterize(Mu \base, Mu \type) {
        my \what := base.^mixin(A[type]);
        what.^set_name(base.^name ~ '[' ~ type.^name ~ ']');
        what
    }
}

dd B[Int];        # B[Int]
dd B[Int].^roles; # (A[Int],)

However, I haven't been able to find a way to make the unparameterized version of B do the unparameterized version of role A.

dd B;        # B
dd B.^roles; # (), want to see (A[Mu],)

I tried to add a ^compose method, but that only gets called if the class is parameterized.

Suggestions very welcome!

like image 237
Elizabeth Mattijsen Avatar asked Jan 13 '19 19:01

Elizabeth Mattijsen


1 Answers

I've come to the conclusion that you can't (at least not in the foreseeable future). So I took a step back to see what I actually wanted to achieve. And that turned out to be possible:

my role A[::T] {
    method foobar() { T }
}

my class B {
    method foobar() { Mu }

    method ^parameterize(Mu \base, Mu \type) {
        my \what := base.^mixin(A[type]);
        what.^set_name(base.^name ~ '[' ~ type.^name ~ ']');
        what
    }
}

dd B.foobar;       # Mu
dd B[Int].foobar;  # Int

The method foobar is by default supplied by the B class. If one parameterizes the B class, it will mix in another version of the foobar method (which shadows the original method), which returns the type given with the parameterization.

In the end, this gave me the framework for allowing parameterization of QuantHashes, a long standing request of my own, and recently others.

like image 73
Elizabeth Mattijsen Avatar answered Nov 10 '22 21:11

Elizabeth Mattijsen