Raku infix operator in method-like syntax
Asked Answered
T

1

10

In Raku, infix operators can be used like functions, e.g.:

1 + 2 ;           # 3
infix:<+>(1, 2) ; # 3
[+] 1, 2 ;        # 3

Prefix operators can be used with method-like syntax (methodop):

-1 ;             # -1
1.:<-> ;         # -1

So, (rather academic) question is, can infix operators also be used in method-like way, like 1.:<+>(2) (which is wrong) ?

Currying

(1 + *)(2) ;     # 3

… that's function (sort of) definition and call, not a method call, nor method-like syntax.

Custom method

my method plus(Int $b --> Int){
  return self + $b;
}

1.&plus(2) ;     # 3

… but + name cannot be used, also that's not direct operator usage without an additional function definition.

Tana answered 15/7, 2021 at 18:47 Comment(0)
F
13

You can use

1.&infix:<+>(2)
1.&[+](2)

1.&(*+*)(2)
1.&{$^a +$^b}(2)
Fiberboard answered 15/7, 2021 at 19:4 Comment(3)
wonderful and logical, I did not fidget enoughTana
… look ma, without parens: 1.&[+]: 2Tana
s/fidget/fiddle/Tana

© 2022 - 2024 — McMap. All rights reserved.