2

In Rascal, how do I introduce a name for a composite type (that's constructed from built-in basic types and type constructors like list and map)? E.g. I'd like to do something like

typedef IntList = list[int];
typedef StrToIntList = map[str,IntList];
StrToIntList mymap = ();
Dennis
  • 171
  • 9

1 Answers1

2

Instead of typedef you can use alias. For instance:

rascal>alias IntList = list[int];
ok
rascal>alias StrToIntList = map[str,IntList];
ok
rascal>StrToIntList mymap = ();
StrToIntList: ()

You can use alias to give alternate names to existing basic types as well, so you could also do something like:

alias ID = int;

And, you can include type parameters if needed, so a graph over an arbitrary type could be defined as:

alias Graph[&T] = rel[&T,&T];

Note that an alias introduces a type equivalence and not just a sub-type. So for any function that accepts the alias as argument type, you can also provide a value of the type that it aliases, or any of its sub-types.

Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
Mark Hills
  • 1,028
  • 5
  • 4