G3M
G3M

Reputation: 1031

Reuse a case class inside an object in scala

I have an object that contains one or more case classes and associated methods. I would like to reuse this case class within another object (which has similar characteristics as this object but also some differentiating methods).

private object abc {
    /* ... */

    case class xyz(..) { def someFunc(){} }

    object xyz { def apply() {} }
}

private object extendabc {

    // How to reuse case class xyz here?
}

Upvotes: 0

Views: 1897

Answers (1)

Som Bhattacharyya
Som Bhattacharyya

Reputation: 4112

If you want to just access you can use this kind of a code .

private object abc {

case class xyz() { 
def someFunc(){} 
}

object xyz {  }

}

private object extendabc {
val a = new abc.xyz()
a.someFunc()
}

You need to call this way because xyz is a nested member of the object abc . Look here.

Also please note you cannot define a apply method in the companion object of a case class as it provides the exact same apply() method (with the same signature.

Upvotes: 1

Related Questions