Expressing a polymorphic behavior with Rascal, and Java-like recursion with Rascal

71 views Asked by At

Say I have a data type defined as follows:

data Constraint=type1(Lab al,Lab a2)| type2(Lab a1,TypeRoot tt)|....

where the general pattern is that the first argument of each constructor is of type 'Lab'. Is there then, an efficient/clear way of expressing a pattern match to do the following?

for (Constraint cc <- constraints)
    if( type1(Lab label,_):= cc || type2(Lab label,_):=cc || .... )
        //do something useful with label. Infact, this snippet of code appears within
        //a function body, and I need to perform the match for a specific label; as follows:
        if(label==labelOfInterest) return cc;

While I'm at this, and against StackOverflow's recommendation of asking one question/thread, does Rascal support simple Java-like recursions?

1

There are 1 answers

3
Paul Klint On

You can check for the presence of a field by way of the has operator. See the Rascal Tutor for details.

Your code would then become something like:

for (Constraint cc <- constraints)
    if( cc has a1 || .... )
        if(cc.label==labelOfInterest) return cc;

or even better:

for (Constraint cc <- constraints)
    if( (cc has a1 && cc.label == labelOfInterest) || .... )
        return cc;