It's a visibility question.
package foo.bar.my_package.my_sub
class MyClass {
var x: SomeType = _
}
When you declare x
, the following things are visible:
- Every member of
MyClass
- Everything inside
foo.bar.my_package.my_sub
- Every member of
scala.Predef
- Everything inside
scala
- Everything inside
java.lang
The type SomeType
is not inside any of those.
On the other hand, when you do:
package foo.bar.my_package
package my_sub
class MyClass {
var x: SomeType = _
}
Then the visibility is:
- Every member of
MyClass
- Everything inside
foo.bar.my_package.my_sub
- Everything inside
foo.bar.my_package
- Every member of
scala.Predef
- Everything inside
scala
- Everything inside
java.lang
And SomeType
is inside foo.bar.my_package
, which is the second line.
A related question is why it works this way. You can get a start here, though there's a question precisely about that. I don't much like the answer to that question, though, as it really doesn't touch the reasons for it. It does link to a page on scala-lang about new features of Scala 2.8, in which Odersky explains the reasons.