sclang allows to call methods in a “functional” style by passing the receiver as the first argument. You typically see this with control flow methods like if or while:
I just found it surprising that there was a special syntax for when you want to do that with an operator. I was looking through the bison file when I found it and though, what on earth is that, but it makes sense given that goal!
It does look confusing, but I think it is mostly caused by fill working with arrays.
I know I just thought it was really confusing syntactically. It seem the only unique behaviour is in setting operator adverbs through arguments. (+)(1, 2, _).
I may have misunderstood your post, but I don’t think this is a special syntax.
The underscore notation (_+_) is indeed (i believe) is just syntax for placeholder arguments, creating a function with implicit parameters. Not sure how far it can pass an operator as a first-class function just using parenthesis. It does not seem to work here.
I have seen more code like this:
~applySymOp = {|symbol, a, b| a.perform(symbol, b)};
~applySymOp.value('+', 5, 3);
Right?
Also works:
~applyOp = {|func, a, b| func.(a, b)};
~applyOp.(_+_, 5, 3);
And:
var add = _+_;
var subtract = _-_;
var multiply = _*_;
var divide = _/_;
add.(5, 3).postln;
subtract.(5, 3).postln;
multiply.(5, 3).postln;
divide.(6, 3).postln;
EDIT: Oh, I read your other message, and you figured that out already.
Exactly, that’s why I did a comparison. In Haskell, an infix operator automatically becomes a prefix operator with parenthesis (that’s just a syntax rule), but is often necessary topass it as first-class function to another function, for example.
It is very consistent in the the other language, not SC.
It would be nice if + could be notated using receiver notation
I think the receiver vs. function call notation distinction primarily applies to methods that are written as identifiers, not those written in the characters (+@/| etc.) reserved for operators. (The messages help file you linked spells out the difference).
So you can do 10 + 4 and (+) (1,4) and these are practically already the equivalent of receiver/function call notation for operators. I guess some methods have aliases, say @ and .at, in that case you’d have the option of spelling out receiver.at(arg) with the period. not sure if there’s much point to that though…
I currently have an overhaul of the syntax shortcuts file in the making, mostly trying to reorganize things such that these distinctions are clearer.
Btw, the [_,_] ! [3,3] case is currently discussed there under “partial application”, which is technically correct/understandable… but misleading because the underscore placeholder is the thing that needs to be explained, but it isn’t the thing that is “partial” about it… for instance f(2) ! 10 is technically a partial application (restriction) of the function f(_) ! 10, but the examples for “partial application” are all of the type f(_), because f(2) doesn’t need to be explained. Maybe there’s a better term for this?