Modern language features
Nat blogged a while ago about Higher-Order Messaging in Ruby. HOM is naturally implemented in Ruby by defining a class to catch calls to undefined methods (c.f. catching a MethodNotFound exception in Java), subclassing it to add logic for particular messages (e.g. 'do', 'where', 'sum'), then calling that logic via the superclass from a popular mixin.
Then, reading a new paper on XML-OO impedance, I came across XLinq examples that reminded me very much of Nat's HOM style. The examples use the .NET Standard Query Operators which rely on extension methods in C# 3.0. An extension method is a static method with a special first parameter indicating the receiver. The static method lives in a static class that the compiler is responsible for finding (for now).
Extension methods are a little odd because it's like multiple dispatch in reverse: string s = new string(); s.echo("hi"); turns into [something like] StringExtension.echo(s, "hi"). Subtyping is presumably stopped in its tracks because given string s = new EchoEnabledString(); s.echo();, the compiler will determine that an extension method echo() is to be invoked, even though dynamically the instance of class EchoEnabledString provides echo(). Of course, without extension methods, s.echo() wouldn't compile at all...but now that it does compile, maintenance programmers will have to realise that EchoEnabledString::echo is never called.
Moreover, extension methods aren't alone in being implemented entirely through the compiler (ahem, a compiler); most C# 3.0 features are "just" syntactic sugar. It's the safe alternative to dynamic languages (ahem, VMs) that punt any non-understood message to user code.
Then, reading a new paper on XML-OO impedance, I came across XLinq examples that reminded me very much of Nat's HOM style. The examples use the .NET Standard Query Operators which rely on extension methods in C# 3.0. An extension method is a static method with a special first parameter indicating the receiver. The static method lives in a static class that the compiler is responsible for finding (for now).
Extension methods are a little odd because it's like multiple dispatch in reverse: string s = new string(); s.echo("hi"); turns into [something like] StringExtension.echo(s, "hi"). Subtyping is presumably stopped in its tracks because given string s = new EchoEnabledString(); s.echo();, the compiler will determine that an extension method echo() is to be invoked, even though dynamically the instance of class EchoEnabledString provides echo(). Of course, without extension methods, s.echo() wouldn't compile at all...but now that it does compile, maintenance programmers will have to realise that EchoEnabledString::echo is never called.
Moreover, extension methods aren't alone in being implemented entirely through the compiler (ahem, a compiler); most C# 3.0 features are "just" syntactic sugar. It's the safe alternative to dynamic languages (ahem, VMs) that punt any non-understood message to user code.

0 Comments:
Post a Comment
<< Home