noncom
noncom

Reputation: 4992

Scala - mixing of packages and variables?

A very strange thing indeed. I have the following project structure:

myproject/one/two

Inside package myproject I have a class:

abstract class A (two: Buffer[Int])

and then, inside package one I have:

object B extends A (Buffer[Int](1, 2, 3)) {

  val с = two.map(_ + 1)  // ERROR

}

However, the erros says:

object map is not a member of package myproject/one/two

which is obviously erroneous because it should be perfectly clear that I don't refer to the packages here, but to the local variable... And two also is not shown in context-assist after this. in B, but is shown in A (Scala-IDE). Is this an intended behavior and I am doing something wrong or is it a bug?

UPDATE:

(simultaneously suggested by Nicolas :D ) Been able to resolve the name collision by specifying two as val (making it public). I did not notice at first, but it was private and unavailable in the successor class. Nevertheless I am still wondering, why and how did Scala pick up a package instead of saying that the variable does not exist or is not accessible?

Upvotes: 1

Views: 104

Answers (1)

Nicolas
Nicolas

Reputation: 24759

It's not as clear as you might think. Without a modifier, two is private to abstract class A class A. Thus your declaration of a is equivalent to abstract class A (private[this] A). It means that field two can't be seen from object B. A direct consequence is that the compiler look into the only defiition of two visible from B: the package two.

Upvotes: 3

Related Questions