Scala seems to transform objects of type String to StringOps. How is this done? I.e. how and when does Scala transform an object of String to StringOps?
-
This is not a programming question, please narrow down to some issue, else it couldl get closed.kebs– kebs2015-11-04 21:00:45 +00:00Commented Nov 4, 2015 at 21:00
-
It actually is a programming question. You just should know Scala to understand it. (Well, no wonder, the question was tagged with "scala" after all.)VasiliNovikov– VasiliNovikov2015-11-04 21:47:05 +00:00Commented Nov 4, 2015 at 21:47
-
This question is not "too broad".pedrofurla– pedrofurla2015-11-04 23:13:44 +00:00Commented Nov 4, 2015 at 23:13
Add a comment
|
1 Answer
There is an implicit conversion defined from String to StringOps in Predef.scala. https://github.com/scala/scala/blob/a24ca7fa617cabada82c43d2d6ac354db698d181/src/library/scala/Predef.scala#L308. This converts a String instance to a StringOps instance if you call method from StringOps class on a String.
2 Comments
VasiliNovikov
That by the way means that StringOps kicks in only when you call it. Otherwise the String is left to be String, no CPU time spent on any transformations.
user1578293
Ok! Got it, thanks! I assume it's the same for all *Ops classes, like ArrayOps etc.