8

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?

3
  • This is not a programming question, please narrow down to some issue, else it couldl get closed. Commented 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.) Commented Nov 4, 2015 at 21:47
  • This question is not "too broad". Commented Nov 4, 2015 at 23:13

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Ok! Got it, thanks! I assume it's the same for all *Ops classes, like ArrayOps etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.