myk*_*hal 10 methods infix-operator raku
在 Raku 中,中缀运算符可以像函数一样使用,例如:
1 + 2 ; # 3
infix:<+>(1, 2) ; # 3
[+] 1, 2 ; # 3
Run Code Online (Sandbox Code Playgroud)
前缀运算符可以与类似方法的语法(methodop)一起使用:
-1 ; # -1
1.:<-> ; # -1
Run Code Online (Sandbox Code Playgroud)
因此,(相当学术的)问题是,中缀运算符是否也可以以类似方法的方式使用,例如1.:<+>(2)(这是错误的)?
(1 + *)(2) ; # 3
Run Code Online (Sandbox Code Playgroud)
……那是函数(某种)定义和调用,而不是方法调用,也不是类似方法的语法。
my method plus(Int $b --> Int){
return self + $b;
}
1.&plus(2) ; # 3
Run Code Online (Sandbox Code Playgroud)
……但+不能使用名称,这也不是没有附加函数定义的直接操作符用法。
wam*_*mba 13
您可以使用
1.&infix:<+>(2)
1.&[+](2)
1.&(*+*)(2)
1.&{$^a +$^b}(2)
Run Code Online (Sandbox Code Playgroud)