Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error occurred in an application involving default arguments" when trying to use Scala's copy method

Tags:

scala

I'm trying to write a convenience function that replaces the left tree of an immutable binary tree, and I'm getting "Error occurred in an application involving default arguments" in the following replaceL method:

abstract class AbNode {
    val key = null
    val value = null

    val leftTree:AbNode = NullNode
    val rightTree:AbNode = NullNode
}

case class Node[K <:Ordered[K],V](k:K, v:V, lT:AbNode, rT:AbNode) extends AbNode {
    val key:K = k
    val value:V = v

    val leftTree:AbNode = lT
    val rightTree:AbNode = rT
}

object Node {
    def replaceL[K <: Ordered[K],V](newTree:AbNode, node:Node[K,V]): Node[K,V] =
        node.copy(leftTree = newTree) //<< Error occurs here
}

case object NullNode extends AbNode {
    val key = null
    val value = null

    val leftTree = NullNode
    val rightTree = NullNode
}
like image 786
Carcigenicate Avatar asked Sep 29 '22 06:09

Carcigenicate


1 Answers

The copy method (and default parameters in general) use the name used in the constructor, not the field name that you assign it to (I don't know why this didn't click sooner).

In the case of a case class, the assigned fields are useless; as far as I can tell, they're simply holding a copy of a reference to the constructor value (not my original intent). I think my confusion stemmed from the fact that in C-style languages, the variables given to a constructor are later assigned to a field. In other words, the way I have my classes set-up is non-sensical, they shouldn't have any fields. My Node class should be simply:

case class Node[K <:Ordered[K],V](k:K, v:V, leftTree:AbNode, rightTree:AbNode) extends AbNode

Which allows copy to see the value I'm referring to.

like image 151
Carcigenicate Avatar answered Oct 24 '22 20:10

Carcigenicate