The Source for Java Technology Collaboration
User: Password:



Ben Galbraith

Ben Galbraith's Blog

C# 3.0: Relational Language Operatings, Type Inference, and More

Posted by javaben on September 13, 2005 at 02:01 PM | Comments (7)

I'm here at Microsoft's Professional Developer Conference (PDC), the Big Redmond's irregularly scheduled conference where they introduce new technology to developers. It's a pretty surreal experience. I'm using the only Mac as far as the eye can see. It's kind of like JavaOne in a parallel universe.

This morning, Bill Gates and Jim Allchin got up and talked about a lot of the old stuff we've been hearing about for years now: Avalon/WPF for graphics, Indigo/WCF for services, etc. For those not following Microsoft, these are really cool technologies, but... old news.

The real interesting action happened when Anders Hejlsberg and Don Box got up and demonstrated Microsoft's LINQ project. It turns out that in a future release of .NET (3.0), Microsoft will embed relational operators into the language itself. To understand what this means, check out this C# code:

public void Linq1() {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var lowNums =
        from n in numbers
        where n < 5
        select n;

    Console.WriteLine("Numbers < 5:");
    foreach (var x in lowNums) {
        Console.WriteLine(x);
    }
}

Err, that's interesting. But hang on, it turns out you can mix and match this code with data from relational databases, all using the same operators. Check this out:

public void Linq1() {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    DataContext context = ...;  // don't worry about how I get a ref to this
    Table<myobject> myTable = context.GetTable<myobject>();

    var lowMyObjects =
        from n in numbers, o in myTable
        where n < 5 and o.MyNumber == n
        select o;

    Console.WriteLine("MyObjects whose number is less than five:");
    foreach (var o in lowMyObjects) {
        Console.WriteLine(o.MyName);
    }
}

Wow. I can seamlessly combine my relational data with my object data. This is just scratching the surface of LINQ (for example, XML is also a first-class type of C# 3); check it out. They've given out pre-release bits; I'll definitely be playing with this stuff.

Oh, did you happen to notice this code snippet in one of the earlier examples?

var lowNums = ...

This is not VB code. C# 3.0 introduces type inference. That's also pretty interesting. Some folks in have been asking why Java doesn't do this for years. That is, if an IDE can figure out how to automatically write the (Cast) operator, why can't the compiler?

I remember asking one of the compiler guys at JavaOne about why they don't introduce type inference in future versions of Java; his answer: "Sounds like you want to use a dynamic language." I'm sure glad some folks understand that you don't need to throw away strong typing to avoid writing the type of a variable over and over again, unnecessarily.

Check out some of the other new features, announced today and coming to some future release sometime in the distant future.

To be clear: this is not a "Run from Java and embrace .NET" post. Rather, I'm excited to see the Java space innovate to keep up with some of these and other really intriguing new C# features. It's fun to watch the game of Java/.NET leap frog play out...

(Check out www.ajaxian.com later today for a blog on some of the Atlas/Ajax stuff that Microsoft announced today...)

(Once again, cross-posted to Married... with Children)


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Really already,

    rather than this interminable rush to copy everything C# into Java; why not just release a Sun C# compiler for the JVM, and be done with it already!

    Why ruin a nice clean language with all this extraneous cr@p.

    PS This is not directed in any way at you Ben; it's just my rant on the recent stewardship of the JLS by Sun.

    Posted by: cajo on September 13, 2005 at 03:38 PM

  • cajo: Interesting idea! Actually, I didn't mean to start a "Java copied .NET -- no wait, .NET copied Java first" thread. ;-)

    Whatever the Java response winds up being, the solution C# 3 proposes to the Object/Relational mismatch is definitely interesting and should influence the coversation.

    You could make a case that Java should freeze and new languages on top of the JVM should handle new features... but I'm not sure that's an entirely necessary position to take.

    Frankly, other than the unfortunate Java implementation of generics, I think most of the .NET inspired features in Java 5 are pretty nice. I'm especially excited about the new JSR to revise class loading along lines similiar to .NET assemblies...

    .NET got a lot of mileage out of copying the whole managed platform idea from Java; there's nothing wrong with Java learning from .NET's innovations and applying them where appropriate.

    Posted by: javaben on September 13, 2005 at 04:09 PM

  • I'm still a little worked up (obviously) because the language features added in 1.5 supposedly required an incompatible class file format change. I work primarily in distributed systems; Java code mobility is being destroyed by this foolishness! BTW: I say "supposedly," because the
    retroweaver project seems to provide the new 1.5 language features while still using the original Java class file format. It sure appears to me that Microsoft has fooled Sun into running in their typical 'moving target' strategy. The one where all parties involved lose; except Microsoft.


    -John

    Posted by: cajo on September 13, 2005 at 04:46 PM

  • Ha, ha. How many syntax and styles must I learn? The concept introduced by Gosling was to provide less, so we could think more abstractly about the problems space. Gosling was against generics/templates due to the extra complexity.
    Why is there such a rush to abstract languages like Ruby? Because Java and C# have too many language features. Hmmm, starting to look like the old C++. :-(
    Last week I took a C# for job. I was surprised that the company was caught up with details and technical parts of the language, rather than concepts like patterns, abstractions, data hiding, and generalized development process. Of course the company is in a desperate situation. But they are so caught up with the details, they can’t abstract out to the real problem and solution.

    Posted by: malcolmdavis on September 13, 2005 at 08:41 PM

  • John: it turns out Java 6 also breaks the bytecode compatibility and no language feature was added. I don't know the inner workings of Java 5 changes but it might not be the reason for that change (even though I doubt so :).

    Posted by: gfx on September 13, 2005 at 11:19 PM

  • This is sad, but not surprising Romain.

    This is why I strongly encourage everyone at my project, and require all developers on my project team:

    Set the -source and -target switches in your build scripts to 1.2

    This assures that your work will run on everything. JDK 1.2 was a watershed release; definitely worth upgrading your JRE from 1.1. After that, it has been just trivial superfluousness; definitely not worth forcing your entire installed userbase to keep updating their JREs. It is time to just unplug, from this ridiculous 'arms race' between Sun and Microsoft; and enjoy the simplicity and power, which is the original promise of Java.

    Posted by: cajo on September 14, 2005 at 05:13 AM

  • It reminds me of XQuery and C-omega.

    C# 3.0 and XQuery

    Posted by: tewk on September 14, 2005 at 07:28 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds