Anonymous Types (like c# or SQL)

Can someone please direct me to the feature(s) of mps typesystem that will allow me to create language that support "Anonymous Types"?


in C#, visual studio + csc.exe (C# compiler) infer that type of the following expression:
new { FirstName = "John", LastName = "Smith" }
prettyPrint(); is a type that contains 2 Fields: "String FirstName" and "String LastName" and provides all the necessary features regarding semantic checks, auto-complete and etc..


in SQL, every FROM clause, JOIN clause and etc, can be modeled as a "chaining" of function that gets "tables" (list of type) and binary expressions as parameter, and type to "project" and returns "list" of type. for example:
select     u.FirstName,
from       Users as u
inner join Cities as c on c.CityId = u.CityId;
prettyPrint(); can be modeled somthing like this:
              {Users u,Cities c => c.CityId == u.CityId},
              {Users u,Cities c => return new {u = u, c = c}}
.Select    (  {UserAndCity => new 
      FirstName = UserAndCity.u.FirstName,
      LastName = UserAndCity.u.LastName,
      CityName = UserAndCity.c.CityName

I read the docs, but I couldn't find what i was looking for. I will be happy if someone get direct me with the following:
  • Can it be done in MPS?
  • I want that for some language construct (Concepts) that their "attached types" will be inferred by a custom algorithm, that will inspect the child nodes and will "create dynamically" a type that is not explicitly defined. Which feature of MPS type-systems can I use to achieve it?

Cheers (:
1 comment
Comment actions Permalink
In MPS you can define the type of each node using the type system rules. How your nodes inference their types is in your hands and there is no restriction, as far as I know. But of course, if the type depends on assignments at different places in your code, you have to walk around in your AST a bit to collect the necessary informations. This can lead to performance problems if you do not use a proper implementation. But for this I'm still missing some hints, too.
If you have already have a look to the type system section in the user guide, you should also read the cook book:

When you work for a while with MPS you will also recognize that many constructs you will use for your language description do not need a type declaration but MPS knows their types immediately.

Please sign in to leave a comment.