Make42
Make42

Reputation: 13088

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?

Upvotes: 0

Views: 41

Answers (1)

Tzach Zohar
Tzach Zohar

Reputation: 37832

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

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

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

Upvotes: 2

Related Questions