- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
access modifiers for type members
Sun, 2011-03-27, 18:11
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
Sun, 2011-03-27, 18:57
#2
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
}
}
On Sun, Mar 27, 2011 at 7:11 PM, Jason Zaugg <jzaugg@gmail.com> wrote:
I'd call it a bug (for me -- yay!). Probably because the type alias gets expanded away before the access is checked.
adriaan