Tuples Support in the MPS Base Language
Many functional and dynamic languages have “tuples” in their syntax. Tuple is a sequence of a fixed length. For example, pair, triple and quadruple are tuples. They are very useful when you have algorithms operating on entities which have two, three or more parts and you don’t want to create devoted classes for them, or if you want to return multiple instances from a method.
Let’s take a closer look at how tuples are implemented in MPS.
We have a tuple type. Here is a pair of String and int:
We have a special syntax for tuple construction: we added a new type of literal to the base language:
Each element of a tuple has a name by which it can be accessed:
Tuples are immutable, so you can only create new tuples but can’t change the existing ones. If two tuples contain same element types but different names they are compatible. I.e. you can write:
It took us only about half a day to implement such a cool features in MPS, so it isn’t really hard to do. This feature is going to be available in MPS Beta 2.
Posted by Konstantin Solomatov, Lead MPS developer