Nemerle and C# 3.0

Mentioned on OS News.
The features mentioned in the C# 3.0 preview, if nothing else, will serve to distance the product from Java still further.
Lambda and query expressions...hmmm...

Comment viewing options

Other notable features

Extension Methods

a la SmallTalk

Instead of making a string class of your own, you can extend it without inheritance or composition. The method "Chomp" does not exist in .NET string type.

----------Extensions.cs-------------
namespace MyCompany.StringUtilities
{
public static class Extensions
{
public static int Chomp(this string s)
{
return s.EndsWith("\n") ? s.TrimEnd("\n".ToCharArray()) : s;
}

}

}

-------MyClass.cs------------
using MyCompany.StringUtilities;

class MyClass
{
public static void Main()
{
string s1 = "test\n";
string s2 = s1.Chomp();
}
}


Lambda Expressions

As taken from the Language Specifications:

x => x + 1	// Implicitly typed, expression body
x => { return x + 1; }	// Implicitly typed, statement body
(int x) => x + 1	// Explicitly typed, expression body
(int x) => { return x + 1; }	// Explicitly typed, statement body
(x, y) => x * y		// Multiple parameters
() => Console.WriteLine()	// No parameters

myObject.MyMethodTakingDelegateParam( x => x + 1);


Type Inference

int i; // you can still use type annotations
var i = 0;  // type inferred as int
var name = "dude";  // type inferred as string;
var question = new[] { "dude", "where's", "my", "car" }; // type inferred string[] array


Anonymous Types

var person = new { Name = "dude", Weight = 150 }
var employees = new[] {
new {
Name = "Chris Smith",
PhoneNumbers = new[] { "206-555-0101", "425-882-8080" }
},
new {
Name = "Bob Harris",
PhoneNumbers = new[] { "650-555-0199" }
}
};


Syntactic Sugar ?

Looking at your first example about "Extension Methods" I wonder if the method is really added to the string class prototype (and in that case how does it types in other parts of program, and what about collisions when two programs are adding the same method ?)

Or is it just syntactic sugar and the s1.Chomp() will translate at compilation time to Chomp(s1) ? That would make more sense but in that case how can you share your extensions among several parts of the code ?

I got lazy. It is imported with a using-namespace directive

I re-expanded the example.

...In effect, imported extension methods appear as additional methods on the types that are given by their first parameter and have lower precedence than regular instance methods...

int Chomp(this string s)

Regarding the line: int Chomp(this string s)
Is "this" really needed?

Wouldn't it be easier just to allow the following:
int Chomp(string s) //usual method signature

Name spaces will take care of method name collisions?

Extending classes in Dylan

In Dylan, a function call x.f is syntactic sugar for f(x). (I think it also works for multi-argument functions, but I don't recall.)