Why can traits in scala.collection create an instance? -
i trying next code in scala, coming an overview of collections api.
import collection._ scala> traversable(1, 2, 3) res5: traversable[int] = list(1, 2, 3) scala> iterable("x", "y", "z") res6: iterable[string] = list(x, y, z) scala> map("x" -> 24, "y" -> 25, "z" -> 26) res7: scala.collection.map[string,int] = map(x -> 24, y -> 25, z -> 26) scala> sortedset("hello", "world") res9: scala.collection.sortedset[string] = treeset(hello, world) scala> indexedseq(1.0, 2.0) res11: indexedseq[double] = vector(1.0, 2.0)
the result shows trait can phone call apply
method create instance of implementation. after looking scala.collection.package
object, found nothing. think there must somewhere binds trait subclass , imports program. can explain is?
you're calling apply
on trait's companion object, not on trait.
for example, traversable
:
the trait
the object
if click on apply
in companion object's scaladoc, can see traversable
object inherits apply method genericcompanion
, has link source can see how it's implemented.
scala collections
No comments:
Post a Comment