Referring to the answer of the following question:
List.empty vs. List() vs. new List()
How did the developers of Scala map the method apply[A](xs: A*)
defined in the List
object , to be usable as List[A](cs: A*)
?
Also how did they translate aListInstance(n: Int)
to the method apply(n: Int)
(which returns the n'th element of the list) defined in the List
class ?
In both cases I'm calling the apply()
methods without writing .apply()
in my code. How does that work?
It works because the Scala Language Specification says so.
foo(bar)
is translated to
foo.apply(bar)
just like
foo.bar = baz
is translated to
foo.bar_=(baz)
and
foo(bar) = baz
is translated to
foo.update(bar, baz)
and
foo bar baz
is translated to
foo.bar(baz)
and
foo bar_: baz
is translated to
baz.bar_:(foo)
and so on and so forth.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.