ERights Home elang / blocks 
Back to: Defining Objects On to: Defining Plumbing

Delegation
using the "extends" keyword


The pattern seen earlier

match [verb, args] {
    E.call(target-expression, verb, args)
}

may often be replaced by the more convenient, which has a similar meaning.

extends super-expression

placed in the head of the object definition. There are two differences between the two:

  • Whereas the target-expression is evaluated on each delegation, the super-expression gets evaluated once at the time the object is evaluated / instantiated. This means that the match / target-expression form is more flexible -- it can determine dynamically where the message will be delegated. Such flexibility is rarely needed, but when it is, the match / target-expression form should be used.

  • The value of the super-expression is bound to the special variable name "super" within the outer scope box.

***Need to show expansion

***Need examples

Here is another alternative category implementation that uses extends to delegate all messages it doesn't understand to the wrapped predicate.

? pragma.syntax("0.8")

? def makeCategory(pred) :any {
>     def category extends pred {
>         to and(other) :any {
>             makeCategory(def _(specimen) :any {
>                 pred(specimen) && other(specimen)
>             })
>         }
>
>         # ... similarly for "or" and "xor"
>
>         to not(other) :any {
>             makeCategory(def _(specimen) :any {
>                 !(pred(specimen))
>             })
>         }
>     }
> }
# value: <makeCategory>

Which creates the same behaviors as before:

? def evenPred {
>     to (num) :any {num %% 2 == 0}
>     to foo() :any {"bar"}
> }
# value: <evenPred>

? def even := makeCategory(evenPred)
# value: <category>

? even(3)
# value: false

? even.foo()
# value: "bar"
 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang / blocks 
Back to: Defining Objects On to: Defining Plumbing
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign