Tutorial: Pattern References


 

Some people view patterns reference attributes (syntactically: <VariablePattern>) as the undisciplined, "goto"ish variant of virtual patterns. True enough, if you can use the more disciplined virtual patterns for a given purpose, by all means do that. On the other hand there is still a place for real, genuine type variables, and that is just what a <VariablePattern> attribute is.

What is a pattern reference?

A <VariablePattern> attribute has a state which is a pattern reference. The only difference between the set of patterns and the set of pattern references is that NONE is included in the set of pattern references. In other words, it is the set of patterns enhanced with a "bottom" element which is used to signify the absence of a pattern value.

The reason behind the terminology "pattern" and "pattern reference" is that this might hint at the analogy with "object" and "object reference." Attributes denoting the "reference" variant may change (what it refers to) over time, whereas attributes denoting the short variant ("pattern"/"object") are immutable: the state of an object may change, but the attribute always denotes the same object.

What does it look like?

The declaration uses the same marker as used in pattern concext coercion, namely "##":

<Names> ":" "##" <AttributeDenotation>

for example:

p: ##object

Example 7

A small example of using a pattern reference attribute:


(* FILE ex7.gb *)
-- betaenv:descriptor --
(# 
   factory:
     (# settype: (# enter type## #);
        counter: @integer;
        type: ##object
     do counter+1->counter
     exit &type[]
     #);
   f: @factory;
   agent: (# do (for 25 repeat '.'->stdio for); '\n'->stdio #)
do 
   agent##->f.settype;
   (for 5 repeat f! for);
   integer##->f.settype;
   (for 5 repeat f! for)
#)

The "!" suffix is a computed object evaluation. The semantics is that the expression in front of it is evaluated. It must deliver one reference to an object (this is checked statically, of course). That object is then executed. The for-imperative is presented later, and so is the explicit object instantiation operator, "&".

Now to something completely different, the next section is about co-routine sequencing.

 


Signed by: eernst@cs.auc.dk. Last Modified: 3-Jul-01