Can anyone explain why Scala gives two different names in the following cases? Why couldn't Scala give the same names in each case?! Is there some kind of consistency I don't know yet? It must be related to eta-expansion, right?
object A {
val ms: Seq[String => String] = Seq(m1)
def m1(s: String) = s
}
A.ms.map(m => m.getClass.getSimpleName)
The above gives List(anonfun$1)
- note the name of the element - anonfun$1
while the following gives anonfun$m1$1
. Why?
object A {
val ms: Seq[String => String] = Seq(m1)
def m1: String => String = s => s
}
A.ms.map(m => m.getClass.getSimpleName)
Could I also ask for a simpler case to demonstrate the difference (perhaps without using Seq
)?
It seems compiler adds path into name when creates anonymous classes. Your example (pretty interesting in some other sense by the way) may be simplified down to:
Which produces:
Without Seq:
Luckily
m1
in Seq is equivalent tom1 _
, andm2
is equivalent to method application. What about names - compiler adds path to autogenerated classes, so, top-scopem1
turns toanonfun$1
(anonfun is default prefix for generated classes for functions), and becausem2
returns function from inside, that function gets one more element in path (name).Interestingly, this weird thing:
Has name:
So, no trace of a, b, c! So, it's somewhat complicated, I tried but couldn't have found the exact choice and patterns of naming in compiler source, though reading was interesting.