REBOL 3.0

Multicontext variable lookup

Carl Sassenrath, CTO
REBOL Technologies
29-May-2008 18:22 GMT

Article #0138
Main page || Index || Prior Article [0137] || Next Article [0139] || 7 Comments || Send feedback

Is there value in providing a method to lookup variables over multiple contexts?

Answer...

The conclusion to this question is posted in blog #140.

This question has recently bubbled to the top from an addition made to the delect function. (Delect mainly provides a high-speed parse method for command mapping, argument order marshalling, and value lookup. It is used for dialects like DRAW.)

There are times when using delect on a block where you want the variable values to be obtained from a specific context or better yet, a collection of contexts.

Delect was expanded to allow:

delect/in dialect input output contexts

Where contexts is a block of contexts to use for resolving the values of variables. An example of this would be in a drawing, where a pen color may be found in one of many style sheets, but also in the primary face object.

This extension of delect raises the question as to whether the technique is generally useful for variable lookup?

For instance, would it be useful to support:

result: reduce/in block contexts

as in:

draw-blk: reduce/in [
    'pen edge-color
    'box 2x2 area-size
][
    face/options frame/options style/options
]

and:

value: get in contexts 'word

as in:

color: get in [
    face/options frame/options style/options 
] edge-color

We might be tempted to think that the equivalent of this is:

color: any [
    face/options/edge-color
    frame/options/edge-color
    style/options/edge-color
]

But, in fact that's not true because if the variable is not in a context, an error will be thrown. Therefore, we would have to write it like this to be safe:

color: any [
    get in face/options 'edge-color
    get in frame/options 'edge-color
    get in style/options 'edge-color
]

Now, if you consider that pattern within a reduced block, it could add up to a significant amount of code, something we don't like a lot of in REBOL.

Where this really seems to be of values is in the implementation of things like property sheets (like CSS in HTML). The pattern is to look for a property in one context, and if not found, look in a parent context, etc.

The overhead of adding this feature to REBOL is small, because it's already implemented for delect.

So, there's the proposal. What do you think?

7 Comments

REBOL 3.0
Updated 26-Dec-2024 - Edit - Copyright REBOL Technologies - REBOL.net