On Wed, 12 Aug 2020, Stuart Hungerford wrote:
> Hi,
> 
> 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=")}

at least on Rakudo. I have no idea if this attribute or the format
for the 'prec' key in particular are standardized.

Best,
Tobias

Reply via email to