Meet the Future of Data Head-on with Comega

Meet the Future of Data Head-on with Comega

first saw Comega about a year an a half ago when the lab that was developing it gave me a sneak preview and asked me for some feedback. The only feedback I could give at the time was “Wow!” Now, everybody can get access to this add-on to C# as Microsoft Research has released the preview for public consumption. What is it, you might ask? Comega is an experimental add-on to C# with the intent to make data a first-class citizen of the language. And it succeeds.

In this article you’ll get a whistle-stop tour of some of the features that Comega adds to C#, and build an example application that uses some of these features!

Comega is a strongly-typed, data-oriented programming language intended to bridge the gap between semi-structured hierarchical data (XML), relational data (SQL), and the common type system (CTS?the root of all variable and type declaration .NET languages). It spans XML, SQL, and the CTS by generalizing how they are used. This is difficult to get your head around just reading about it?and best learned through practical and hands-on examples. A little later you will build an application that uses some of these features and gives you a taste for what you can do in Comega.

Getting Started
You can download Comega from Microsoft Research. You’ll need Visual Studio.NET 2003 to be able to use it. Once it’s installed, when you fire up Visual Studio and open the Create New Project dialog, you’ll see a new item in the Project Types list for Comega, and if you select it you can see the Comega project templates. (See Figure 1.)

Go ahead and create a new ‘Windows Application’ with Comega. This creates a new project of type ‘cwproj‘ containing ‘form.cw‘ which is a simple form written with Comega.

Open up this form and take a look at its code. You can see some of this code in Listing 1.

The first thing that you’ll notice is that you have XML inline in your code! It’s not hidden within a string where syntax coloring cannot be interpolated?it’s right there in your code and is used to set the properties of the controls. It’s a little weird at first, because as a developer you are so used to having the XML outside of your code and having to parse the XML to do something meaningful. With Comega, data is a first-class citizen, so XML is used like any other keyword.

Figure 1. Creating a New Comega Project: Once you’ve installed Comega, you create a new Comega project like you would any project in Visual Studio?by selecting a project template from the New Project dialog.

Run this application and you’ll get a basic white window with a salmon-colored button that closes the window if you press it.

Using Data in Comega
The core type systems of Comega are:

  • Streams:
    These are central to how Comega works. A stream is (not to be confused with File Streams or the like) a homogenous collection of a particular type that is constructed when it is needed, and as such, is much more flexible than an array. A stream is used similarly to a struct, but what is special about it is that it can contain programming logic. So, for example, if you want to create a stream that contains even numbers, you can declare it like this:
    int* staticEvenNumbers()		{			yield return 2;			yield return 4;			yield return 6;	} 

    This is very similar to how you would declare a struct. C++ programmers will think pointers when they see the ‘*’ but don’t worry, you’re not going back into pointer trouble! The neat thing about the Comega stream is that you don’t need to statically declare the numbers, it can also be done programmatically, such as:

    int* dynamicEvenNumbers(int nCount)		{			for (i=0; i<=nCount; i++) yield return i*2;	}

    What you get is the best of both worlds between a function call and a data structure! DynamicEvenNumbers is an unusual, and very flexible beast now. It behaves like a function call?returning nCount number of even numbers, a data structure that contains those numbers, and a dynamic array that sizes itself without you having to worry about it. Cool huh?

  • Content Classes:
    A content class is a class that is used to specify the schema or structure of a document type. It is analogous to an XML DTD or an XSD sequence. In practical use, this means that if you want to generate XML documents in your Comega program, you have the additional flexibility of automatic validation of their types. Traditionally you would use a DOM to build the document manually, twiddling with nodes and attributes, and then hope for the best. In Comega, life is a lot easier.

    For example, if you want to create a Content Class for an e-mail message, it needs to contains a header and a body. The header must have a 'From' and a 'To' field, and optionally a 'Subject' field. The body is a string containing the text. You would declare the content class like this:

    public class Email {   //content of Email message  struct{    struct{       string From;       string To;       string? Subject;    } Header;    struct{        string P;    }+ Body;}

    The usefulness of this isn't immediately apparent, but, when you combine it with a function for generating an e-mail message that uses inline XML, it'll reveal itself. Here's an example of such a function:

    public static Email Vacation(DateTime d, TimeSpan s, string to){    return              
    John Doe {to} OOF

    I am OOF from {d} until {d+s}.

    ;}

    Now you are generating an XML document of type 'Email,' which will be validated by the content class. This is much easier than twiddling around with XMLDocuments, XMLNodes, and such. In addition, you enter the custom data for the e-mail message directly inline in the XML by passing the parameters such as 'to' to the XML using the {} syntax, so {to} will create the correct node.

  • SQL-like Selection:
    You are almost certainly accustomed to using SQL for getting data from tables in a database, and have found that there is some wonderful logic that can be applied in SQL to filter and sort information. With Comega, you can now do this with your in-memory data structures. You'll start with a simple stream, for example:
    :struct{ string Title; string Actor; string Genre;}* DVDs = new{ new{Title="The Matrix", Actor="Keanu", Genre="SciFi"},     new{Title="Battlestar Galactica", Actor="Edward James Olmos", Genre="TVSciFi"},     new{Title="Blade Runner", Actor="Edward James Olmos", Genre="SciFi"},     new{Title="Pride and Prejudice", Actor="Colin Firth", Genre="Mushy"}};

    And next, you can query this stream:

    results = select * from DVDs where Actor=="Keanu";results.{Console.WriteLine("Title = {0}, Genre={1}", it.Title, it.Genre);}; 

    So, you can now start using your SQL skills to pull out data instead of writing your own iterators!

These are but a few of the features that are offered in the language and are here to whet your appetite. Comega offers a lot more, including, but not limited to, a new concurrency model for multithreaded applications (formerly known as polyphonic C#), transactions support in SQL, choice-types, nullable value types, and anonymous types. You can learn a lot more in the Comega documentation that comes with the install.

Putting it Together in a Simple Application
The source code download has the full code for this sample article, or you can step through what's covered here. The first thing that you need to understand when building a database application is that Comega can take the metadata from a database into a managed assembly using the Sql2Comega.exe tool that is in the BIN directory of the Comega install. You'll see how this helps you in a moment. First, create a new 'Northwind' Comega application, and follow the instructions in the comments at the top of the generated code. This will get the Northwind connection working using the Sql2Comega tool mentioned earlier. Next, add a new class called 'GetData' to the project.

The entire GetData file is shown in Listing 2.

Change the output type for the project from Console Application to Class Library. You do this by right-clicking on the project in solution explorer, selecting properties, and then selecting 'General' under 'Common Properties'. On the right-hand side you set the project Output. (See Figure 2.)

If you compile your project now, a DLL assembly will be created for you.

Author's Note: Sometimes you will get strange database errors when compiling this (and other) Comega applications. If you encounter these, regenerate the Northwind reference as you did earlier, and try again. If that fails, close Visual Studio, then reopen it and compile again. This software is not a release software, so bugs are inevitable.

You can now consume this assembly within a traditional C# application. Create a new C# Web service and create a reference to the DLL. Note that you cannot create a multi-project solution, mixing C# and Comega in the current version. When you have referenced this DLL, add the following Web method to your application.

 [WebMethod]public string getData(){   ComegaGetData.DataBlock[] x = new ComegaGetData.DataBlock[32];   x.Initialize();   ComegaGetData.GetData dService = new ComegaGetData.GetData();   try   {     x = dService.getData();   }   catch (Exception e)   {     Console.Write(e.Message);   }     return "Hello"; }
Figure 2. Setting the Output Type for a Project: The right column in this table allows you to change many properties of your project. In this case, change the output type to Class Library.

This will consume your Comega content classes (the DataBlocks) and load them into an array for C# to use. You can access the data using (for example) the following syntax. You can see (thanks to the struct) that it is strongly-typed.

x[1].sequence.Body.ProductName

The method just returns 'Hello' when it is done?you could extend it to process the data, now stored in X as you see fit.

This article gave you a whistle-stop tour of some of the features of Comega, and showed you how to build Comega applications today that may be consumed by your front-end applications built in traditional C# or VB.NET. The examples clearly show the power of the language, but have barely scratched the surface of what is possible. Use the techniques shown here to start using Comega meaningfully today to build a data layer into your applications. This is an excellent and innovative language that has a bright future and it will be very interesting to see it unfold and evolve. It isn't likely to hit the mainstream for some time yet?probably not until C# version 3 at the earliest?but you can use it today!

devx-admin

devx-admin

Share the Post:
AI Girlfriend Craze

AI Girlfriend Craze Threatens Relationships

The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious

AIOps Innovations

Senser is Changing AIOps

Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the

Malyasian Networks

Malaysia’s Dual 5G Network Growth

On Wednesday, Malaysia’s Prime Minister Anwar Ibrahim announced the country’s plan to implement a dual 5G network strategy. This move is designed to achieve a

Advanced Drones Race

Pentagon’s Bold Race for Advanced Drones

The Pentagon has recently unveiled its ambitious strategy to acquire thousands of sophisticated drones within the next two years. This decision comes in response to

Important Updates

You Need to See the New Microsoft Updates

Microsoft has recently announced a series of new features and updates across their applications, including Outlook, Microsoft Teams, and SharePoint. These new developments are centered

AI Girlfriend Craze

AI Girlfriend Craze Threatens Relationships

The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious repercussions for America’s future. A

AIOps Innovations

Senser is Changing AIOps

Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the root causes of service disruptions

Bebop Charging Stations

Check Out The New Bebob Battery Charging Stations

Bebob has introduced new 4- and 8-channel battery charging stations primarily aimed at rental companies, providing a convenient solution for clients with a large quantity of batteries. These wall-mountable and

Malyasian Networks

Malaysia’s Dual 5G Network Growth

On Wednesday, Malaysia’s Prime Minister Anwar Ibrahim announced the country’s plan to implement a dual 5G network strategy. This move is designed to achieve a more equitable incorporation of both

Advanced Drones Race

Pentagon’s Bold Race for Advanced Drones

The Pentagon has recently unveiled its ambitious strategy to acquire thousands of sophisticated drones within the next two years. This decision comes in response to Russia’s rapid utilization of airborne

Important Updates

You Need to See the New Microsoft Updates

Microsoft has recently announced a series of new features and updates across their applications, including Outlook, Microsoft Teams, and SharePoint. These new developments are centered around improving user experience, streamlining

Price Wars

Inside Hyundai and Kia’s Price Wars

South Korean automakers Hyundai and Kia are cutting the prices on a number of their electric vehicles (EVs) in response to growing price competition within the South Korean market. Many

Solar Frenzy Surprises

Solar Subsidy in Germany Causes Frenzy

In a shocking turn of events, the German national KfW bank was forced to discontinue its home solar power subsidy program for charging electric vehicles (EVs) after just one day,

Electric Spare

Electric Cars Ditch Spare Tires for Efficiency

Ira Newlander from West Los Angeles is thinking about trading in his old Ford Explorer for a contemporary hybrid or electric vehicle. However, he has observed that the majority of

Solar Geoengineering Impacts

Unraveling Solar Geoengineering’s Hidden Impacts

As we continue to face the repercussions of climate change, scientists and experts seek innovative ways to mitigate its impacts. Solar geoengineering (SG), a technique involving the distribution of aerosols

Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their Razer Blade 17 model. Typically

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations with industry professionals. The gathering,

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and Dell Technologies Inc. more time

Semiconductor Stock Plummet

Dramatic Downturn in Semiconductor Stocks Looms

Recent events show that the S&P Semiconductors Select Industry Index seems to be experiencing a downturn, which could result in a decline in semiconductor stocks. Known as a key indicator

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This decision demonstrates Amazon’s commitment to

AI Experts Get Hired

Tech Industry Rehiring Wave: AI Experts Wanted

A few months ago, Big Tech companies were downsizing their workforce, but currently, many are considering rehiring some of these employees, especially in popular fields such as artificial intelligence. The

Lagos Migration

Middle-Class Migration: Undermining Democracy?

As the middle class in Lagos, Nigeria, increasingly migrates to private communities, a PhD scholar from a leading technology institute has been investigating the impact of this development on democratic

AI Software Development

ChatGPT is Now Making Video Games

Pietro Schirano’s foray into using ChatGPT, an AI tool for programming, has opened up new vistas in game and software development. As design lead at business finance firm Brex, Schirano

Llama Codebot

Developers! Here’s Your Chatbot

Meta Platforms has recently unveiled Code Llama, a free chatbot designed to aid developers in crafting coding scripts. This large language model (LLM), developed using Meta’s Llama 2 model, serves

Tech Layoffs

Unraveling the Tech Sector’s Historic Job Losses

Throughout 2023, the tech sector has experienced a record-breaking number of job losses, impacting tens of thousands of workers across various companies, including well-established corporations and emerging startups in areas

Chinese 5G Limitation

Germany Considers Limiting Chinese 5G Tech

A recent report has put forth the possibility that Germany’s Federal Ministry of the Interior and Community may consider limiting the use of Chinese 5G technology by local network providers

Modern Warfare

The Barak Tank is Transforming Modern Warfare

The Barak tank is a groundbreaking addition to the Israeli Defense Forces’ arsenal, significantly enhancing their combat capabilities. This AI-powered military vehicle is expected to transform the way modern warfare

AI Cheating Growth

AI Plagiarism Challenges Shake Academic Integrity

As generative AI technologies like ChatGPT become increasingly prevalent among students and raise concerns about widespread cheating, prominent universities have halted their use of AI detection software, such as Turnitin’s