Reputation: 3101
Initially I had a case class inside an object. So I could call extractSales
method from another class like this: SaleProcessor.extractSales(salesJson)
.
object SaleProcessor {
case class Sale(name: String, type: String, description: String) {
def extractSales(salesJson: JValue): Seq[Sale] = {
salesJson.extract[Seq[Sale]]
}}}
Then I read somewhere that there is no need to nest a case class inside an object and decided to remove the SaleProcessor
object leaving just the case class. But after that I'm unable to call the method like before.
As far as I understand without an object I have to instantiate the case class. But not sure how to achieve this since the Sale object is created at the moment if extraction from JSON?
Another question is what would be the most adequate approach with nesting case classes into objects and what is considered the best practice in Scala?
Upvotes: 2
Views: 6077
Reputation: 170735
You'd still keep the method inside the object
and call it the same way:
case class Sale(name: String, type: String, description: String)
object SaleProcessor {
def extractSales(salesJson: JValue): Seq[Sale] = ...
}
// elsewhere
SaleProcessor.extractSales(salesJson)
It would be common to name the object the same as the class (i.e. object Sale
instead of object SaleProcessor
), making it a companion object, but not necessary.
Upvotes: 7