3

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.

Jaap
  • 3,081
  • 2
  • 29
  • 50
  • 2
    Are you sure you want Inner to be *instance* class, rather than *class* class (in Java you write *static* for the later)? If not you have to define Inner in companion, otherwise `val x = Outer(1); x.Inner(2)` will work – om-nom-nom Oct 29 '13 at 15:33

1 Answers1

10

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)
Kkkev
  • 4,716
  • 5
  • 27
  • 43
volia17
  • 938
  • 15
  • 29
  • 3
    See also [*type projection*](http://stackoverflow.com/questions/6676048/why-does-one-select-scala-type-members-with-a-hash-instead-of-a-dot) – om-nom-nom Oct 29 '13 at 15:49