Scala case class prohibits call-by-name parameters?
I have also not found why exactly by-name parameters are prohibited in case classes. I guess explanation should be quite elaborate and complex. But Runar Bjarnason in his book "Functional Programming in Scala" provides a good approach to handle this obstacle. He uses the concept of a "thunk" together with memoizing. Here is an example of Stream implementation:
sealed trait Stream[+A]
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
object Stream {
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
lazy val head = hd
lazy val tail = tl
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*))
}
}
As you see, instead of a regular by-name parameter for the case class data constructor they use what they call a "thunk", a function of zero-arguments () => T
. Then to make this transparent for the user they declare a smart constructor in the companion object which allows you to provide a by-name parameters and make them memoized.
This is actually similar approach to the Stream
solution but simplified to what is actually required:
case class A(x: () => Int) {
lazy val xx = x()
}
So you can use your case class as:
def heavyOperation: Int = ???
val myA = A(heavyOperation)
val myOtherA = A(() => 10)
val useA = myA.xx + myOtherA.xx
Like this the actual heavy operation will be performed only when you use xx
, i.e., only on the last line.
There is no contradiction: class A(x: => Int)
is equivalent to class A(private[this] val x: => Int)
and not class A(private val x: => Int)
. private[this]
marks a value instance-private, while a private-modifier without further specification allows accessing the value from any instance of that class.
Unfortunately, defining a case class A(private[this] val x: => Int)
is not allowed either. I assume it is because case-classes need access to the constructor values of other instances, because they implement the equals
method.
Nevertheless, you could implement the features that a case class would provide manually:
abstract class MyList[+T]
class MyNode[T](val h: T, t: => MyList[T]) extends MyList[T]{
def getT = t // we need to be able to access t
/* EDIT: Actually, this will also lead to an infinite recursion
override def equals(other: Any): Boolean = other match{
case MyNode(i, y) if (getT == y) && (h == i) => true
case _ => false
}*/
override def hashCode = h.hashCode
override def toString = "MyNode[" + h + "]"
}
object MyNode {
def apply[T](h: T, t: => MyList[T]) = new MyNode(h, t)
def unapply[T](n: MyNode[T]) = Some(n.h -> n.getT)
}
To check this code, you could try:
def main(args: Array[String]): Unit = {
lazy val first: MyNode[String] = MyNode("hello", second)
lazy val second: MyNode[String] = MyNode("world", first)
println(first)
println(second)
first match {
case MyNode("hello", s) => println("the second node is " + s)
case _ => println("false")
}
}
Unfortunately, I do not know for sure why call-by-name val and var members are prohibited. However, there is at least one danger to it: Think about how case-classes implement toString
; The toString
-method of every constructor value is called. This could (and in this example would) lead to the values calling themselves infinitely. You can check this by adding t.toString
to MyNode
's toString
-method.
Edit: After reading Chris Martin's comment: The implementation of equals
will also pose a problem that is probably more severe than the implementation of toString
(which is mostly used for debugging) and hashCode
(which will only lead to higher collision rates if you can't take the parameter into account). You have to think carefully about how you would implement equals
to be meaningfull.