You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As reported in the Scala-User mailing list (~2011-07-26T13:09), protected[packagename] on trait doesn't work, ie. the trait seems to be public.
I paste here my observations:
object X
{
def main(args: Array[String])
{
val mc = new MyClass
mc.doBar()
}
}
D:\Temp\Scala
C:\Java\scala-2.8.1.final\bin\scalac.bat T1.scala
D:\Temp\Scala
C:\Java\scala-2.8.1.final\bin\scalac.bat T2.scala
T2.scala:5: error: trait MyTrait cannot be accessed in package c.d
class MyClass extends MyTrait
^
T2.scala:7: error: not found: value doFoo
def doBar() { doFoo(); println("Bar") }
^
two errors found
D:\Temp\Scala
C:\Java\scala-2.9.0.final\bin\scalac.bat T1.scala
D:\Temp\Scala
C:\Java\scala-2.9.0.final\bin\scalac.bat T2.scala
D:\Temp\Scala
C:\Java\scala-2.9.0.final\bin\scala.bat a.b.X
Foo
Bar
I took a code snippet from the online Programming in Scala [1] to activate the protection, ie. to get a compilation error:
package bobsrockets
{
package navigation
{
private[navigation] class Navigator
{
protected[navigation] def useStarChart() {}
class LegOfJourney
{
private[Navigator] val distance = 100
}
private[this] var speed = 200
}
}
package launch
{
import navigation._
object Vehicle
{
private[launch] val guide = new Navigator
def vroom() = guide.useStarChart
}
}
}
D:\Temp\Scala
C:\Java\scala-2.8.1.final\bin\scalac.bat T3.scala
T3.scala:20: error: class Navigator cannot be accessed in package bobsrockets.navigation
private[launch] val guide = new Navigator
^
one error found
D:\Temp\Scala
C:\Java\scala-2.9.0.final\bin\scalac.bat T3.scala
error: scala.MatchError: <unknown tree of class class scala.tools.nsc.typechecker.Infer$Inferencer$AccessError> (of class scala.tools.nsc.typechecker.Infer$Inferencer$AccessError)
at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:754)
at scala.tools.nsc.transform.TypingTransformers$TypingTransformer.transform(TypingTransformers.scala:53)
at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:243)
at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:831)
[...] (long stack trace)
t4845.scala
package p.q {
protected[p] trait T {
def t = 42
}
}
t4845b.scala
package v.w {
class C extends p.q.T {
def c = 27
}
object Test extends App {
println {
new C().t
}
}
}
yields indeed
t4845b.scala:3: error: trait T in package q cannot be accessed as a member of package p.q from package w in package v
Access to protected trait T not permitted because
enclosing package w in package v is not a subclass of
package q in package p where target is defined
class C extends p.q.T {
^
or
3 | class C extends p.q.T {
| ^^^^^
| trait T cannot be accessed as a member of p.q.type from class C.
| protected[p] trait T can only be accessed from package p.
I did not test whether dotty actually handles access differently.
As reported in the Scala-User mailing list (~2011-07-26T13:09), protected[packagename] on trait doesn't work, ie. the trait seems to be public.
I paste here my observations:
package c.d
protected[d] trait MyTrait
{
def doFoo() { println("Foo") }
}
package a.b
import c.d.MyTrait
class MyClass extends MyTrait
{
def doBar() { doFoo(); println("Bar") }
}
object X
{
def main(args: Array[String])
{
val mc = new MyClass
mc.doBar()
}
}
D:\Temp\Scala
D:\Temp\Scala
D:\Temp\Scala
D:\Temp\Scala
D:\Temp\Scala
I took a code snippet from the online Programming in Scala [1] to activate the protection, ie. to get a compilation error:
package bobsrockets
{
package navigation
{
private[navigation] class Navigator
{
protected[navigation] def useStarChart() {}
class LegOfJourney
{
private[Navigator] val distance = 100
}
private[this] var speed = 200
}
}
package launch
{
import navigation._
object Vehicle
{
private[launch] val guide = new Navigator
def vroom() = guide.useStarChart
}
}
}
D:\Temp\Scala
D:\Temp\Scala
[1] http://www.artima.com/pins1ed/packages-and-imports.html "Public members" (no anchors, alas)
The text was updated successfully, but these errors were encountered: