1

I'm using scala 2.11.0

I notice StringBuilder is defined in scala.collection.mutable.StringBuilder and java.lang.StringBuilder. I also notice scala implicitly import java.lang., scala., scala.Predef._

Why am I using scala.collection.mutable.StringBuilder and not java.lang.StringBuilder when I write

val def = new StringBuilder

thanks

3 Answers3

3

You can check what scala import via looking into package.scala

Lines 74-45 imports StringBuilder by

type StringBuilder = scala.collection.mutable.StringBuilder
val StringBuilder = scala.collection.mutable.StringBuilder
Hüseyin Zengin
  • 1,216
  • 11
  • 23
2

Well because everything here is available by default. i.e. scala._ is by default available.

In that package object you can see:

 type StringBuilder = scala.collection.mutable.StringBuilder

This is why doing new StringBuilder takes scala.collection.mutable.StringBuilder and not java.lang.StringBuilder

Jatin
  • 31,116
  • 15
  • 98
  • 163
0

Scala uses type aliasing to ensure that core scala types are available without an import.

https://github.com/scala/scala/blob/27da46343cd545534819300235bc64ab74958c92/src/library/scala/package.scala#L74 will take you to the type alias declaration.

This link will also help - Understanding what 'type' keyword does in Scala

Note that since the type alias declaration is in the Scala package object, and scala._ is imported implicitly, it bring the type alias declaration into the scope for every compilation unit.

Community
  • 1
  • 1
S V
  • 570
  • 8
  • 21