3 ms·
> This is a real hairy example IMO since it's conflating the mechanics with optional parenthesis and dot-less method calls, but we can break it down just the sa
by breuleux 12y ago
> This is a real hairy example IMO since it's conflating the mechanics with optional parenthesis and dot-less method calls, but we can break it down just the same. Since Int.+ takes a single argument, then it must parse as:
> 1.+(_ * 2)
No, my point is that this is not how it parses. Look:
ary.map(1 + (_:Int) * 2)
res3: Array[Int] = Array(3, 5, 7, 9, 11)
ary.map(1.+((_:Int) * 2))
error: overloaded method value + with alternatives:
(Double)Double
...
In the first case, the parser lifts _ over $times AND over $plus. The function is being lifted over both of them. The optional parentheses break the feature if you insert them: "1.+(_ * 2)" tries to add one to a function, which is not a valid operation.
>> how much of the context does it grab along with it?
> None. In this position, it's the beginning of an expression. It doesn't escape the scope it's defined in. Just like in the 1.+(_ * 2) example, what comes before it doesn't matter. It's the beginning of an expression.
If that was true, then "1 + _ * 2" would be invalid, and "f(_)(1)" would be equivalent to "f(1)". But the former is valid and the latter doesn't work like that.
> I was going to attempt to write a Ruby analogue, but I can't actually figure out a clean way to do that.
There is a limited Python analogue:
https://github.com/kachayev/fn.py#scala-style-lambdas-definition https://github.com/kachayev/fn.py#scala-style-lambdas-defini...
That only works insofar that the _ placeholder object can seize control over the expression, so "f(_, 2)" won't work unless f explicitly handles _. Implementing the full feature would require language or macro support, but my point was more that a new dynamic language could trivially support it.