Last active
June 11, 2016 15:06
-
-
Save pedrofurla/d399c76c68c9b4e03283fe121c7afbb5 to your computer and use it in GitHub Desktop.
Where String#+ comes from?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Tmp { | |
"zzz" ++ "kkk" // Fails with -Yno-predef | |
// after typer the above is scala.this.Predef.augmentString("zzz").++[Char, String](scala.this.Predef.augmentString("kkk"))(scala.this.Predef.StringCanBuildFrom); | |
"aaa"+new Object() // Doesn't fail ever! | |
// after typer the above is "aaa".+(new java.this.lang.Object()); | |
// Why didn't `+` desugar to something else? | |
new Object() + "aaa" // Fails with -Yno-predef | |
// after typer the above is scala.this.Predef.any2stringadd[Object](new java.this.lang.Object()).+("aaa"); | |
// import Predef.{any2stringadd => _, StringAdd => _, augmentString => _, _} | |
} |
@demobox, it doesn't explains how to compiler ends up compiling it into StringBuilder#append.
- https://github.com/scala/scala/blob/v2.11.8/src/reflect/scala/reflect/internal/Definitions.scala#L1061
- https://github.com/scala/scala/blob/v2.11.8/src/compiler/scala/tools/nsc/backend/ScalaPrimitives.scala#L215
- https://github.com/scala/scala/blob/v2.11.8/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala#L233
- https://github.com/scala/scala/blob/v2.11.8/src/compiler/scala/tools/nsc/backend/jvm/BCodeIdiomatic.scala#L209-L239
Awesome, cool!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://www.scala-lang.org/files/archive/spec/2.11/12-the-scala-standard-library.html#class-string:
"Scala's String class is usually derived from the standard String class of the underlying host system (and may be identified with it). For Scala clients the class is taken to support in each case a method
which concatenates its left operand with the textual representation of its right operand."