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

accessing one's inner trait

2 replies
richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.

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

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: accessing one's inner trait

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
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
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 { ... }

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