develooper Front page | perl.perl6.users | Postings from August 2020

Re: Combining multiple "is..." traits into one?

Thread Previous | Thread Next
From:
Stuart Hungerford
Date:
August 11, 2020 22:15
Subject:
Re: Combining multiple "is..." traits into one?
Message ID:
CAG+kMrGNvd5VF2JEvQScbscH1M_B5iCgYp9ZQfRJpqAJ_NVXzw@mail.gmail.com
On Wed, Aug 12, 2020 at 8:12 AM Tobias Boege <tobs@taboege.de> wrote:

> > I'm working with roles where the mixing-in classes and roles need to
> > have some mixin-specific notion of equality. So I have an Equality
> > role that can also be
> > mixed in as needed:
> >
> > role Equality {
> >   method equal(Equality:D) of Bool:D {...}
> > }
> >
> > To make using the role easier I've created an infix operator "=/=". To
> > give the infix operator predcitable semantics I need to specify the
> > precedence and the associativity with traits:
> >
> > sub infix:<=/=>(Equality:D \x, Equality:D \y) of Bool:D is
> > equiv(&infix:<==>) is assoc<chain> is export {
> >   x.equal(y)
> > }
> >
> > Is there some way to combine traits to a more succinct trait?  Something like:
> >
> > subset Equivalence of Routine where {magic}...;
> >
> > sub infix:<=/=>(Equality:D \x, Equality:D \y) of Bool:D is equivalence
> > is export;
> >
> > Would a custom version of the trait_mod:<is> routine do the trick?
>
> Yes:
>
>   multi trait_mod:<is> (Routine $r, :$equivalence!) {
>       trait_mod:<is>($r, :equiv(&infix:<==>));
>       trait_mod:<is>($r, :assoc<chain>);
>   }
>
> At the same time you can mix in a role if you need to, by adding
> `$r does Equivalence`. If you want to query this information of a
> predefined operator to make a subset, check for instance
>
>   dd &infix:<+>.prec
>   # Hash element = {:assoc("left"), :prec("t=")}

Nice!

Thanks for that insight.

Stu

Thread Previous | Thread Next


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