Tuesday, January 23, 2007

Predefined extensions

Freya will have to implement something like C# 3.0's extension methods. I'm not an extension method's fan, and some details still wait for a decision. Meanwhile, we have added some predefined extension methods to the predefined double type, so you can write this:
property Length: Double;
begin
Result := (x.Sqr + y.Sqr + z.Sqr).Sqrt;
end;
Every method defined in the static System.Math class can be used as an instance method on any System.Double instance. On the other hand, there's no Math.Sqr method at all: it's just an intrinsic method, requiring some compiler magic. Another "intrinsic" method extension: the Ord method applied to a Char expression.
Another addition to the compiler: a "peephole" optimizer. This is an optimizer that works on a IL representation. Right now, the optimizer choose between short and long branch codes, eliminates jumps in cascade, remove some useless data writes (most of them induced by the Result variable) and detects unreachable code.

Labels:

Friday, January 05, 2007

Collection initializers

Freya have another new feature borrowed from C# 3.0: collection initializers.
var L := new List[Integer]![1, 2, 3, 4];
As you can see, an array literal can be appended to the creation of any class implementing the generic ICollection[T] interface type. But, this time, the array literal doesn't generates an array at runtime. Instead, for each item in the literal, a call to the method Add, from the collection, is generated.
In order to realize why we need collection initializers, you must weight the alternative method:
var L := new List[Integer]([1, 2, 3, 4]);
In this case, the array literal is passed as a parameter to the constructor call. However, the parameter is declared as IEnumerable[T]. Can you see what it means? A temporal array must be generated and initialized, item by item. Then, the array must be casted as a IEnumerable[T] reference, which will be passed to the constructor. Inside the constructor, GetEnumerator is called, and a second temporal object is created. Finally, a loop is performed, in order to add an item for each enumerated item.

Labels: ,