Search code examples
reflectionpropertiesrascal

Rascal MPL dynamically get properties / fields of variable


I often find myself testing using typeOf whenever I'm lost. Is there a similar way to print all properties or fields of a variable, similar to Object.keys (JavaScript) or vars (Python)?


Solution

  • To print all the current keyword fields of a constructor:

    import Node;
    iprintln(getKeywordParameters(myInput));
    

    I use that a lot to print the fields of an M3 model, for example. However, that does not print the fields that have not been set. Rascal is a statically typed language, and there is information which can be retrieved which is not present in the value (default fields for example).

    If I want to know the general signature of a function or constructor, I type the name in the console, then click on its source location:

    rascal>println
    value: choice(
      [
        function(|file:///Users/jurgenv/git/rascal/src/org/rascalmpl/library/IO.rsc|(11118,784,<418,0>,<455,36>)),
        function(|file:///Users/jurgenv/git/rascal/src/org/rascalmpl/library/IO.rsc|(11904,69,<457,0>,<458,27>))
      ],
      [])
    

    That works for constructors are well, so you can see which positional fields they have or keyword fields.

    To find out the name of the current constructor:

    import Node;
    getName(myValue);