Domanda

Considering this typedef:

case class Outer(someVal: Int) {
  case class Inner(someOtherVal: Int)
}

how do I construct an object of type Inner, (i.e. how do I write the valid scala syntax)?

I want the Inner to scoped to Outer to avoid name clashes with different instances of Outer in the same package.

È stato utile?

Soluzione

Inner is in scope of an outer instance. So, you can write something like that :

val res = new Outer(4)
val res2 = new res.Inner(2)

But, i don't think it's what you want. To avoid name clashes, you can use package, it is made for that.

edit :

You can also define Inner in companion object of Outer, like om-nom-nom said :

case class Outer(someVal : Int)
object Outer {
  case class Inner(otherVal : Int)
}

val res = Outer(5)
val in = Outer.Inner(6)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top