The precise semantics of the plumbing expression is still being settled. Most E programmers can and should avoid it for now. This is an experimental construct, but is safe and allowed. It can be turned on using a pragma: ? pragma.syntax("0.8") ? pragma.enable("plumbing") This form is just like the following object expression def param { match pattern { matcher-expression } } except in the plumbing expression, all messages are passed to the matcher. The corresponding object-expression implicitly contains the Miranda methods, like __printOn, effectively before the match clause. LazyMaker was an earlier example of delegation in an object expression. The lazy object was supposed to act like the result of the postponed calculation -- in this case a number, but it printed out as "<lazy>" instead of a number. Why? Since we used an object expression, the Miranda methods were donated to the lazy object. The lazy object responded to the __printOn method itself, rather than delegating it. Here's a LazyMaker that delegates everything to the result of the postponed calculation. ? def LazyMaker(postponed) :any { > var value := null > var hasValue := false > def lazy match [verb, args] { > if (hasValue) { > value > } else { > hasValue := true > value := postponed() > } > E.call(value, verb, args) > } > } # value: <LazyMaker> Before using it, let's again define our num variable and numThunk function: ? var num := 0 # value: 0 ? def numThunk() :any { num } # value: <numThunk> ? def lazyNum1 := LazyMaker(numThunk) # value: 0 ? num += 1 # value: 1 ? lazyNum1 + 0 # value: 0 Just by defining it at the top level of our command interpreter, we cause it to be printed, which causes the postponed calculation to happen. Not very useful, let's try again. ? def lazyNum2 := LazyMaker(numThunk); "foo" # value: "foo" lazyNum2's calculation is still postponed. ? num += 1 # value: 2 ? lazyNum2 # value: 2 ? num += 1 # value: 3 ? lazyNum2 # value: 2 Note that currently, "0 + lazyNum" will not work, as 0 does not see lazyNum as a fellow number. Repairing this is known as transparent bottoming out, but will probably not happen by version 1.0.x of the language. |
||||||||||||
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
|