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.

1
  • 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 Commented Oct 29, 2013 at 15:33

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.