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

Can't access protected static inner classes of extended classes

2 replies
Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 21 hours ago.

This looks like a bug to me, since it works in Java.

Given a Java class defined like this:

package com.example;

public abstract class Test{
protected static class Inner{}
protected abstract Inner getInner();
}

It is not possible to implement this class in Scala from another package:

Welcome to Scala version 2.7.3.final (OpenJDK Server VM, Java 1.6.0_0).
scala> import com.example.Test
import com.example.Test

scala> class Implementor extends Test{
| import Test.Inner
| def getInner:Inner = null
| }
:7: error: class Inner cannot be accessed in object com.example.Test
def getInner:Inner = null
^

Aside from that Inner is not in scope as you would expect it to be:

scala> class Implementor extends Test{
| def getInner:Inner = null
| }
:6: error: not found: type Inner
def getInner:Inner = null
^

I'm wondering why no one stumbled over this case yet. Am I missing
something? Any workarounds?

Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Can't access protected static inner classes of extended cl

It's even worse in trunk. Just got a crash. :)

I'd report a bug.

2009/3/19 Johannes Rudolph :
> This looks like a bug to me, since it works in Java.
>
> Given a Java class defined like this:
>
> package com.example;
>
> public abstract class Test{
>        protected static class Inner{}
>        protected abstract Inner getInner();
> }
>
> It is not possible to implement this class in Scala from another package:
>
> Welcome to Scala version 2.7.3.final (OpenJDK Server VM, Java 1.6.0_0).
> scala> import com.example.Test
> import com.example.Test
>
> scala> class Implementor extends Test{
>     | import Test.Inner
>     | def getInner:Inner = null
>     | }
> :7: error: class Inner cannot be accessed in object com.example.Test
>       def getInner:Inner = null
>                    ^
>
> Aside from that Inner is not in scope as you would expect it to be:
>
> scala> class Implementor extends Test{
>     | def getInner:Inner = null
>     | }
> :6: error: not found: type Inner
>       def getInner:Inner = null
>                    ^
>
> I'm wondering why no one stumbled over this case yet. Am I missing
> something? Any workarounds?
>
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>

Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 21 hours ago.
Re: Can't access protected static inner classes of extended cl

Filed as #1806: https://lampsvn.epfl.ch/trac/scala/ticket/1806

Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

On Thu, Mar 19, 2009 at 5:11 PM, Ricky Clarkson
wrote:
> It's even worse in trunk.  Just got a crash. :)
>
> I'd report a bug.
>
> 2009/3/19 Johannes Rudolph :
>> This looks like a bug to me, since it works in Java.
>>
>> Given a Java class defined like this:
>>
>> package com.example;
>>
>> public abstract class Test{
>>        protected static class Inner{}
>>        protected abstract Inner getInner();
>> }
>>
>> It is not possible to implement this class in Scala from another package:
>>
>> Welcome to Scala version 2.7.3.final (OpenJDK Server VM, Java 1.6.0_0).
>> scala> import com.example.Test
>> import com.example.Test
>>
>> scala> class Implementor extends Test{
>>     | import Test.Inner
>>     | def getInner:Inner = null
>>     | }
>> :7: error: class Inner cannot be accessed in object com.example.Test
>>       def getInner:Inner = null
>>                    ^
>>
>> Aside from that Inner is not in scope as you would expect it to be:
>>
>> scala> class Implementor extends Test{
>>     | def getInner:Inner = null
>>     | }
>> :6: error: not found: type Inner
>>       def getInner:Inner = null
>>                    ^
>>
>> I'm wondering why no one stumbled over this case yet. Am I missing
>> something? Any workarounds?
>>
>> Johannes
>>
>> -----------------------------------------------
>> Johannes Rudolph
>> http://virtual-void.net
>>
>

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