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

Re: Avoiding monkey typing for custom roles on built-in types...

Thread Previous | Thread Next
From:
Ralph Mellor
Date:
August 9, 2020 23:24
Subject:
Re: Avoiding monkey typing for custom roles on built-in types...
Message ID:
CAPLR5Sc2_JoXSV-BrWvfWvSHQWYAFQM=+Sif5iw+F_qNm172wA@mail.gmail.com
> Would a better approach be to create custom subtypes
of the built-in numeric types that mixin the algebraic roles?

It could maybe work out better to mix roles into numeric instances:

role G {
  has Numeric $numeric handles *;
  has ThreeDObject[:Rotation] $threedobject-rotation handles *;
  method G { "Gotta like groups" }
}
given 42 does G { say .G, .abs } # Gotta like groups42

Can you see something like that working out?

----

Maybe sweeten it up:

sub postfix:<G> (\obj) { obj does G }
given 42G { say .G, .abs } # Gotta like groups42

----

NB Declaring an op like that is SLOW. So make sure you're
declaring it just once per test run, not once per individual test.

It *kinda* worked to turn it into a macro to avoid the run-time hit:

use experimental :macros;
macro postfix:<G> (\obj) { quasi { {{{obj}}} does G } }
given 42G { say .G, .abs } # Gotta like groups42

but there's a warning displayed on stderr for the above.

----

Remember you can parameterize G:

role G[:$ass, :$ide, :$inv, :$com] { ... }
given 42 does G[:ass] { say .G, .abs } # Gotta like groups42

I'm not saying that makes any sense whatsoever for what
you want to do, but it seemed worth mentioning.

----

As another tip that may or may not be relevant, but one which is
far more wildly out-of-left-field than role parameterization, indeed
almost a non-sequitur in the context of this discussion, I've found
out that if a NON parameterized role has exactly one public attribute,
you can set its value as part of mixing in a role:

role foo { has $.bar }

given 42 does foo(99) { say .bar } # 99

----

Thoughts?

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