Make42 Make42 - 1 month ago 8
Scala Question

Passing a class instance with a private constructor (Factory Pattern) to a function

In one of my Scala files I have

object DataObject {

def create(...) {
new DataObject(...)
}

}

private case class DataObject(...) { ... }


using a Factory pattern. Can I not give DataObject as a input parameter to

object myfunction { value(in: DataObject): Double = ??? }


? I get the error from IntelliJ that "Cannot resolve symbol DataObject". If I remove
private
I don't get the error anymore, but I don't understand why I can't pass
DataObject
with
private
. I thought it is only about the constructor? Also what can I do to pass
DataObject
afterall?

Answer
private case class DataObject(...) { ... }

makes the class private; If you want to make the constructor private:

case class DataObject private(...) { ... }
Comments