access modifiers for type members

27 views
Skip to first unread message

Jason Zaugg

unread,
Mar 27, 2011, 1:11:14 PM3/27/11
to scala-l...@googlegroups.com
Given this trait, with a private type member:

scala> trait X { private[this] type A = Int; def a: A = 1 }
defined trait X

The access restrictions correctly prevent me from accessing the type
member from outside of `x`.

scala> type xA = x.A
<console>:10: error: type A is not a member of java.lang.Object with X
type xA = x.A
^
scala> val x = new X {}
x: java.lang.Object with X = $anon$1@58bf7b3e

scala> x.a
res6: Int = 1

But I can see the type in the eta-expanded signature here:

scala> x.a _
res7: () => x.A = <function0>

Not sure if this is a problem, it just strikes me as a bit odd to
allow private type members to appear in public signatures, by contrast
with the way private template definitions are handled.

scala> trait X { private[this] trait A; def a: A = null }
<console>:8: error: private trait A escapes its defining scope as
part of type X.this.A
trait X { private[this] trait A; def a: A = null }
^
Any insights into the distinction?

Thanks,

-jason

Adriaan Moors

unread,
Mar 27, 2011, 1:40:38 PM3/27/11
to scala-l...@googlegroups.com, Jason Zaugg


On Sun, Mar 27, 2011 at 7:11 PM, Jason Zaugg <jza...@gmail.com> wrote:
Any insights into the distinction?
I'd call it a bug (for me -- yay!). Probably because the type alias gets expanded away before the access is checked.

adriaan

Jason Zaugg

unread,
Mar 27, 2011, 1:47:48 PM3/27/11
to adriaa...@epfl.ch, scala-l...@googlegroups.com

What's your diagnosis with protected, rather than private? That's
actually the case I found in the wild that prompted my investigations.

GridGain's Scala API contains:

class ScalarCacheProjection[K, V](private val impl:
GridCacheProjection[K, V]) extends
Iterable[GridCacheEntry[K, V]] {
assert(impl != null)

protected type Projection = K GridCacheProjection V
// ...
def update(k: K, v: V): Projection = {
impl.putx(k, v)

impl
}
}

Reply all
Reply to author
Forward
0 new messages