"While trying to use Scala copy method" An error occurred in the application with default arguments "

I am trying to write a convenience function that replaces the left tree of an immutable binary tree and I get "Error occurred in an application involving default arguments"

in the following method replaceL

:

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
}

      

+3


source to share


1 answer


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

In case, the case class

assigned fields are useless; as far as I can tell, they just keep a copy of the constructor value reference (not my original intention). I think my confusion is due to the fact that in C-type languages, variables passed to a constructor are subsequently assigned to a field. In other words, the way I set my classes is insensitive, they shouldn't have fields. My Node class should be simple:



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

      

Which lets you copy

see the meaning to which I mean.

+2


source







All Articles