Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: perl6 custom operator problem

by bduggan (Pilgrim)
on Mar 29, 2017 at 02:24 UTC ( [id://1186298]=note: print w/replies, xml ) Need Help??


in reply to perl6 custom operator problem

A few ideas:
  • A few enums could make sense in this situation, depending on what syntax you are aiming for
  • I'm not sure that you need to specify the associativity and precedence when they are the same as the default for those operations
  • A postcircumfix operator would allow you to send things like 'kg' as a parameter
  • I'm not sure you need to list the submethod BUILD since it behaves like the default
  • You can avoid some redundancy in defining your operators if Mass and Length have a common ancestor

Here's another version that accomplishes something similar:
enum Prefixes ( :pico(10.0 ** -12), :nano(10.0 ** -9), :micro(10.0 ** -6), :milli(10.0 ** -3), :centi(10.0 ** -2), :one(1.0), :hecto(10.0 ** 2), :kilo(10.0 ** 3), :mega(10.0 ** 9), :giga(10.0 ** 9), :tera(10.0 ** 12), ); enum MassAbbrevs ( :g(one), :kg(kilo) ); enum LengthAbbrevs ( :m(one), :km(kilo) ); class SI { } class Mass is SI { has $.v; } class Length is SI { has $.v; } multi postcircumfix:<[ ]>(Real $n, MassAbbrevs $abbrev ) { return Mass.new(v => $n * $abbrev); } multi postcircumfix:<[ ]>(Real $n, LengthAbbrevs $abbrev ) { return Length.new(v => $n * $abbrev); } multi infix:<+>( SI $x , SI $y where .isa($x) ) { $x.WHAT.new( v => $x.v + $y.v); } say 10[kg] + 1[g]; say 1[km] + 1[m];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-24 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found