Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
mykhal Avatar asked Jul 15 '21 18:07

mykhal


People also ask

Do non-infix operators have the same associativity in raku?

However, for operators built in to Raku, all operators with the same precedence level also have the same associativity. Setting the associativity of non-infix operators is not yet implemented. In the operator descriptions below, a default associativity of leftis assumed. Operator classification

How to call an operator in prefix form like a method?

An operator in prefix form can still be called like a method, that is, using the .methodop notation, by preceding it by a colon. For example: my $a = 1; say ++$a; # OUTPUT: «2␤» say $a.:<++>; # OUTPUT: «3␤» Technically, not a real operator; it's syntax special-cased in the compiler, that is why it's classified as a methodop. methodop .::

What is the difference between method Postfix and infix?

These operators are like their Method Postfix counterparts, but require surrounding whitespace (before and/or after) to distinguish them. infix .= Calls the right-side method on the value in the left-side container, replacing the resulting value in the left-side container.

What is the difference between Type 2 and infix function call?

2 shr 1 + 2 and 2 shr (1 + 2) 1 until n * 2 and 0 until (n * 2) xs union ys as Set<*> and xs union (ys as Set<*>) On the other hand, infix function call have higher precedence than the boolean operators && and ||, is- and in-checks, and some other operators.


Video Answer


1 Answers

You can use

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

1.&(*+*)(2)
1.&{$^a +$^b}(2)
like image 197
wamba Avatar answered Oct 29 '22 04:10

wamba