- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
accessing one's inner trait
Wed, 2010-10-13, 23:55
Is there a means of getting at a trait's an inner trait with some
way other than using a '$'?
trait A {
trait B {
def stuff: Unit
}
def doCB(b: B): Unit
}
abstract class Foo extends A {
// ...
}
abstract class Bar extends A$B {
// ...
}
/*
object Scope1 {
import A
class Hoe extends B {
}
}
object Scope2 {
import A._
class Dee extends B {
}
}
*/
Note the "A$B" in class Bar. I tried various importing statement
based upon KISS (Keep It Simple Scala ©) but failed.
Richard
Thu, 2010-10-14, 01:27
#2
Re: accessing one's inner trait
On Wed, Oct 13, 2010 at 03:55:39PM -0700, richard emberson wrote:
> Is there a means of getting at a trait's an inner trait with some way
> other than using a '$'?
First of all, don't even be tempted to use a $.
> abstract class Bar extends A$B { }
Not cheating your way to implementation details, this would be
abstract class Bar extends A#B
and scala would sensibly give you the kibosh:
:6: error: A is not a legal prefix for a constructor
abstract class Bar extends A#B
^
Think of A#B as an infinite universe of types. Your concrete superclass
can't be a whole universe, it has to be just one.
val stableValue = new A { ... } // here you create a specific universe
import stableValue._ // here you import a specific "B"
class Dee extends B { ... }
You have to extends A in order to get access to B:
object Container extends A {
class Bar extends B { ... }
...
}
On Wed, Oct 13, 2010 at 6:55 PM, richard emberson
wrote:
> Is there a means of getting at a trait's an inner trait with some
> way other than using a '$'?
>
> trait A {
> trait B {
> def stuff: Unit
> }
> def doCB(b: B): Unit
> }
> abstract class Foo extends A {
> // ...
> }
> abstract class Bar extends A$B {
> // ...
> }
> /*
> object Scope1 {
> import A
> class Hoe extends B {
> }
> }
> object Scope2 {
> import A._
> class Dee extends B {
> }
> }
> */
>
> Note the "A$B" in class Bar. I tried various importing statement
> based upon KISS (Keep It Simple Scala ©) but failed.
>
> Richard
> --
> Quis custodiet ipsos custodes
>