- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange ambiguity error for imported private members
Wed, 2011-12-14, 05:39
Hi,
Why does importing a private field lead to an ambiguity? In the below code Bar.baz should not be visible in FooBar and hence baz should unambiguously refer to Foo.baz:
object Foo { val baz = "foo"}
object Bar { private val baz = "foo"}
object FooBar { import Foo._ import Bar._ val fooBar = baz}
test$ fsc test.scala test.scala:12: error: reference to baz is ambiguous;it is imported twice in the same scope byimport Bar._and import Foo._ val fooBar = baz
ThanksHeiko
--
Heiko Seeberger
Twitter: hseeberger
Blog: heikoseeberger.name
Company: Typesafe - Enterprise-Grade Scala from the Experts
Author of Durchstarten mit Scala, a German tutorial-style Scala book
Why does importing a private field lead to an ambiguity? In the below code Bar.baz should not be visible in FooBar and hence baz should unambiguously refer to Foo.baz:
object Foo { val baz = "foo"}
object Bar { private val baz = "foo"}
object FooBar { import Foo._ import Bar._ val fooBar = baz}
test$ fsc test.scala test.scala:12: error: reference to baz is ambiguous;it is imported twice in the same scope byimport Bar._and import Foo._ val fooBar = baz
ThanksHeiko
--
Heiko Seeberger
Twitter: hseeberger
Blog: heikoseeberger.name
Company: Typesafe - Enterprise-Grade Scala from the Experts
Author of Durchstarten mit Scala, a German tutorial-style Scala book
The only access modifier that prevents a name from being importable, and hence subject to ambiguity is private[this].
"4.7 The import expression determines a set of names of im- portable members of p which are made available without qualification. A member m of p is importable if it is not object-private.
It's definitely a bit of a gotcha when you expect people to wildcard import from your API (I made this mistake with Scalaz 6.0.2).
-jason