I am working through a coursera example of Signal and cannot make sense of this syntax,
class Signal[T](expr: => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
The update method is written as
protected def update(expr: => T): Unit = {
myExpr = () => expr
computeValue()
}
I can understand that expr is being passed by name so is evaluated only when called.
But what i cannot get my head around is why is the myExpr represented as () => T ?
Also why is the assignment written as myExpr = () => expr . From what i understand () => expr denotes a Function0 that returns expr.
I think my understanding of byname is wrong. Can someone please elaborate on this ?
Or can we rewrite the above syntax as follows,
class Signal[T](expr: () => T) {
import Signal._
private var myExpr: () => T = _
private var myValue: T = _
private var observers: Set[Signal[_]] = Set()
private var observed: List[Signal[_]] = Nil
update(expr)
And the update method as ,
protected def update(expr: () => T): Unit = {
myExpr = expr
computeValue()
}