Jaap Jaap - 11 days ago 4x
Scala Question

Scala syntax how to create an instance of a nested case class

Considering this typedef:

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

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

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


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)