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

Retrofit built-in types with custom roles?

From:
Stuart Hungerford
Date:
December 31, 2019 23:29
Subject:
Retrofit built-in types with custom roles?
Message ID:
CAG+kMrF10KF=gbYVQpdwGbWm-ABY6c__D1Uxo_ShaW=z0TZFDw@mail.gmail.com
Hi,

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
this?

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

Thanks,

Stu



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About