Search code examples
rascal

Is it possible to define a tuple with a reference to itself (recursive)


I'm trying to make a tuple which optionally has a list of tuples of the same type.

alias MyTuple = tuple[str name, list[MyTuple] children];

The code is failing as MyTuple isn't know at this moment.

Is this possible, or do we need to use a Node or Construct to get this structure?


Solution

  • You seem to be trying to define a recursive data type. Something similar to what you write is this:

    data MyTuple = myTuple(str name, list[MyTuple] children);
    

    With this you can construct arbitrarily nested values of type MyTuple.

    An alias is just a shorthand notation for another type and cannot be used for a recursive definition since the type you refer to has to be defined beforehand.