blue-sky
blue-sky

Reputation: 53916

Overriding case class constructor

When attempt to overload constructor argument of case class :

 case class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
    def this(name : String) = this(isVisited , adjacentNodes , name)
  }

receive this error : not found: value isVisited

Should this not work as explained in accepted answer of :

Overload constructor for Scala's Case Classes?

However,this works, although not using a case class :

 class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
        def adjacentNodes(): scala.collection.mutable.MutableList[Node] = { adjacentNodes }
        def name(): String = { name }
      }

      object Node {
        def apply(name: String): Node = new Node(false, scala.collection.mutable.MutableList[Node](), name)
      }

Upvotes: 0

Views: 514

Answers (1)

Lee
Lee

Reputation: 144206

isVisited and adjacentNodes do not exist for the overloaded constructor. It looks like you intend to use false and an empty list if they are not provided:

case class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
    def this(name : String) = this(false, scala.collection.mutable.MutableList[Node](), name)
}

Upvotes: 3

Related Questions