I would think that the parentheses surrounding "x1.toArray" would isolate it syntactically from the "(0)" ...
x1.toArray.apply(0)
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I would think that the parentheses surrounding "x1.toArray" would isolate it syntactically from the "(0)" ...It seems it should but actually it doesn't.
I wonder why scala compiler works in this way, but if you need to write it in one line the only work-around I know is
x1.toArray.apply(0)
I suppose the method is converted to a function,Hello,
Because it would explain why extra parentheses don't make a difference.
Yeah, but it isn’t actually a correct explanation, as we can verify as follows:
scala> import scala.reflect.runtime.{universe => u}
import scala.reflect.runtime.{universe=>u}
scala> def foo(x: Int)(y: String) = y * x
foo: (x: Int)(y: String)String
scala> u.reify { (foo(3))("bar") }
res5: reflect.runtime.universe.Expr[String] = Expr[String]($read.foo(3)("bar"))
scala> u.reify { foo(3)("bar") }
res6: reflect.runtime.universe.Expr[String] = Expr[String]($read.foo(3)("bar"))
The resulting AST is the same regardless of whether the parentheses are present or not.
This is what we ought to expect anyway, since eta expansion doesn't take place unless a function type is expected, but there's no reason here for a function type to be expected. So eta expansion only happens if we explicitly ask for it:
scala> (foo(3) _)("bar")
res7: String = barbarbar
We can also arrive at the same conclusion by inspecting the generated bytecode:
scala> object O { (foo(3))("bar") }
defined object O
scala> :javap O$
…
0: aload_0
1: invokespecial #13 // Method java/lang/Object."<init>":()V
4: aload_0
5: putstatic #15 // Field MODULE$:LO$;
8: getstatic #20 // Field .MODULE$:L;
11: iconst_3
12: ldc #22 // String bar
14: invokevirtual #26 // Method .foo:(ILjava/lang/String;)Ljava/lang/String;
17: pop
18: return
...