This page is no longer maintained — Please continue to the home page at www.scala-lang.org

access modifiers for type members

2 replies
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.

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
: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 =

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 }
: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

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: access modifiers for type members


On Sun, Mar 27, 2011 at 7:11 PM, Jason Zaugg <jzaugg@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
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: access modifiers for type members

On Sun, Mar 27, 2011 at 7:40 PM, Adriaan Moors wrote:
>
>
> On Sun, Mar 27, 2011 at 7:11 PM, Jason Zaugg 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

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
}
}

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland