Reputation: 3482
In "Programming in Scala" (Section 24.15, Views), I saw next code:
scala> val v = Vector(1 to 10: _*)
v: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
v.view map (_ + 1)
res69: scala.collection.SeqView[Int,Seq[_]] = SeqViewM(...)
It's said
... is in essence a wrapper that records the fact that a map with function (_ + 1) needs to be applied on the vector v.
But I can't figure out where those SeqViewR*M*S* are. Are there any already created classes, like Function1..22 (what I didn't find, and it's a bit impossible to create all combinations), or these are created dynamically?
Upvotes: 1
Views: 69
Reputation: 13667
SeqViewR
, SeqViewRMS
, etc. are not in fact classes, but only text descriptions for views. The same class will use a different description depending on how much transformation it represents, e.g. SeqViewR
and SeqViewRR
are usually the same class.
The relevant code is the viewToString
method in TraversableViewLike
:
protected[this] def viewIdString: String = ""
protected[this] def viewIdentifier: String = ""
def viewToString = stringPrefix + viewIdString + "(...)"
stringPrefix
is the original type of the view, e.g. SeqView
. viewIdString
is the R
/RMS
/ whatever part. When you invoke a method like map
or take
or reverse
, you obtain a class that implements TraversableViewLike.Transformed
, which has the following definition:
final override protected[this] def viewIdString = self.viewIdString + viewIdentifier
and then in each implementation something like:
final override protected[this] def viewIdentifier = "R"
This viewIdString
method takes the viewIdString
of the original view and attaches the appropriate letter. This works recursively, so the result of view.reverse.reverse.reverse
is a class with a viewIdString of "RRR"
. Note that no classes are being dynamically generated, only their descriptions.
Upvotes: 2