rascal

What is the most up to date IsDefined operator in Rascal?


According to Rascal's documentation, the "?" operator can be used to query if a variable is "defined".

For example:

int u=1;
int v; // Defined but uninitialised

u = v?2;

v is uninitialised and therefore u will get the value 2.

However, doing this flags a "Warning: deprecated feature: run-time check on variable initialisation"

Hence the question, what is the non-deprecated way to do what the ? operator did in Rascal?


Solution

  • You can check with the IsDefined operator only things that can in principle be "undefined". Variables are not in that class; they were accidentally and now we are deprecating that behavior. In principle, there exists no null or undefined value in Rascal.

    Having said that there are situations with maps and keyword fields of nodes and algebraic constructors where it is possible that a declared name does not exist at runtime. So:

    myMap[myKey]?def; // a map does not have to have the key
    myCons.myKeywordField?def ; // a keyword field does not have to be set
    

    The isDefined operator is part of the assignment syntax on the left-hand side, as explained here: https://www.rascal-mpl.org/docs/Rascal/Statements/Assignment/IsDefined/

    Also, the same syntax can be used as an expression: https://www.rascal-mpl.org/docs/Rascal/Expressions/Values/Boolean/IfDefinedElse/

    Again, checking variables for undefinedness does not make sense since variables are always defined in Rascal. It is a static error otherwise. Defined but uninitialized variables are for making matching patterns look more elegant:

     int i; int j; // here they are declared with a type
     // here they are not defined and may not be used
     if (<i, j> := <1,2>) { // here they are bound/defined
        // here they can be used
     }
     // here i and j are not defined again and may not be used