methodsrakuinfix-operator

Raku infix operator in method-like syntax


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.


Solution

  • You can use

    1.&infix:<+>(2)
    1.&[+](2)
    
    1.&(*+*)(2)
    1.&{$^a +$^b}(2)