Friday, March 25, 2005 8:52 PM bart

Adventures in Comega - part 1 (Introduction)

In the upcoming days/weeks I'll be posting more about Comega on an (ir)regular basis. This first post is meant as a general introduction in the Comega project.

What is it?

Comega (abbreviated as Cw, w standing for the Greek letter "omega") is a research programming language that is created by Microsoft Research and contains a bunch of new language features that will likely (partially) make it into C# v3.0. The homepage of Cw can be found on http://research.microsoft.com/Comega/.

How to get it?

You can get the "Comega compiler preview 1.0.2" on the website mentioned above. It will integrate with Visual Studio .NET 2003 and it will install some samples too to introduce the language.

Why a new language?

Well, it's not really a new language, it's rather a collection of new language features. Summarized in one sentence, Cw is focusing mainly on briding the gap between various data models (formerly known as X# or Xen), including the relational model, the object model and XML. The overall idea is to extend C# with language constructs that make it easier to program against structured relation data and semi-structured XML data. But there is more than just this:

  • Streams, iterator functions and apply-to-all-expressions allow you to do similar things as with sequences in XPath and XQuery. In fact, part of this has already been realized in C# v2.0 with generic collections. As the matter in fact, one of the differences is that C# is now aware of the concept of streams (that are declared using *, which looks somewhat like C/C++ pointers at first sight). Multiple streams cannot be embedded but will be flattened into one stream.
  • Content classes are in fact declared as structs and are the C#-language equivalent for DTDs in the XML space. The idea is to declare "content" that is semi-structured based on a struct definition that is extended with *, + and ? "flags" that work in a similar fashion as these operators in regular expressions (optional, one or more, zero or one). By declaring such a type, you can use XML directly in the language to declare an instance of that type and to perform various operations on it (this feature of embedding XML in code is also referred to as XML literals in Cw).
  • Nullable types introduces kind of a ternary logic in the language, allowing value types to be null too (I call it ternary because of the sample of having a bool? type - no the ? is not a typo - that can have values true, false and null). This means that a null reference can be propagated to its properties (e.g. retrieving the length of a string which is set to null returns null too).
  • Choce types are the equivalent of the XSD xs:choice element in the XML world. Basically it's very similar to union types in C/C++ where the start address of more than one variable (type) is the same (thus values that are projected on the same memory location are sitting "on top of each other"). Because of the static checks of the compiler, you can assign to the choice type without referencing the internal type (e.g. choice{int; string;} x = 1; will set the int value whereas choice{int; string; } y = "bart" will set the string value).
  • Anonymous structs are the same as xs:sequence declarations in XSD. Basically it just means that you can declare a struct that has various types in it and you can just assign to it without having to use field names or whatsoever (e.g. struct {string; string; double price} allows you to instantiate an instance using new {"Duvel", "Beer", 2.50 } and to take advantage of it by using indexers and properties for the named fields).
  • Built-in query operators for XPath. As you can use XML inside the Cw language directly, you need a way to query the "variables" too. In order to do so, the 'dot' operator plays the same role as XPath queries (Products.Product.Price maps to '/Products/Product/Price') and of course this is strongly-typed. However, there is more. When using the * operator you retrieve a stream (same operator to declare and use streams). This way you can get all the products by using Products.Product.*, which is a stream container that you can iterate over and so on. Last but not least the [...] notation allows you to perform selections on the objects, returning a stream with the found elements.
  • Built-in query operators for SQL. To bridge the gap between object-oriented design and relational databases, Cw supports database objects that represent a relational database on some server somewhere. Queries can then be done (that is, after importing the schema) by using built-in operators such as select. The big advantage of this is the static typing at compile time and the fact that you can avoid classic O/R mapping tools (cf. O/R tools, ObjectSpaces, etc). The syntax is the same as SQL (select ... from ... where ... order by ... group by ... having ..., support for joins etc) but it's using classic operators such as &&, ||, ! to create logical expressions.
  • SQL DML statements in Cw. As you have query statements, you also have DML statements (i.e. update, insert, delete) in Cw, also in a strongly typed fashion. There's also built-in transaction support using the keywords transact, commit and rollback associated with a database object (looks somewhat like try...catch...finally or using block structures).
  • Concurrency is also tightly integrated in Cw. That means you'll find the notion of synchronous and asynchronous methods (async keyword, allows you to create thread without using a thread library or async constructions from the .NET Framework directly). Beside of that, there's the notion of a chord. Explained very briefly (in more detail later on in a future post) a chord is a list of methods that is associated with a method declaration. Semantically, a chord tells the system that the method in question can only be executed if all the chords have been called previously. By using this mechanism, synchronisation issues can be solved rather declaratively than by using classic constructs like semaphores. In fact, this is the field of a subset of Cw (in fact it's the other way around, Cw is a merge of various projects) called Polyphonic C# (see http://www.research.microsoft.com/%7Enick/polyphony/).

Interesting kick-off readings include http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml01142005.asp and http://www.infoworld.com/article/05/03/22/HNcomega_1.html which was released to the web very recently.

Check out my blog the upcoming days/weeks for more information to come and some samples of Comega.

UPDATE: All my Comega posts will be listed on http://community.bartdesmet.net/blogs/bart/category/40.aspx too.

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

No Comments