develooper Front page | perl.perl6.users | Postings from December 2019

Retrofit built-in types with custom roles?

Stuart Hungerford
December 31, 2019 23:29
Retrofit built-in types with custom roles?
Message ID:

I'm a complete Raku novice and I'm still at the stage of understanding
how the major language features are used idiomatically.

As an exercise I'd like to do the equivalent of Haskell's typeclass
implementation for the built in numeric types.  I understand this may
well be a misguided exercise!

Raku Roles look ideal for capturing parametric polymorphic behaviour,
e.g. an additive group:

    role AddGroup[::A] {
      # methods omitted for now.

Ideally the built-in Int, Rat etc classes could retro-actively "do"
the role, in this style, using invalid Raku:

    class Int does AddGroup {...}  # Not valid Raku.

I can see a pragma called MONKEY-TYPING that could be relevant but
sounds like it's not intended to be used for day to day problems like

Can anyone point me towards a more idiomatic Raku way of doing this?


Stu Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About