Ceylon's cool features are too addictive for my own good

I have been playing around with Ceylon after hearing UnFromage singing its praises and it is very addictive.

In my mind there is no contest the C# language is much, much more streamlined and expressive than Java7


I am already wishing C# used some of Ceylon's cool features, namely union types.
Consider the following scenario where we use the template method pattern.
    public abstract class Handler<T>
        // do some stuff but don't let the implementor decide of the ordering of actions
        public void DoIt(T value)
            // we're handling the ordering here!
            //  Implementor only provides ways to start and complete
        protected abstract T Start(T value);
        protected abstract void Complete(T value);

Now,  suppose we want to provide a handler that does satisfy both intand double. We have to write two implementations, as C# doesn't do multiple inheritance. If Foo and Bar's implementation are similar we've got to break-down the handler in a third common class. Moar clutter. Don't like it.

Introducing Ceylon's union types!

A union type is defined as being any of the possible types in the union. The type system works its magic retrieves the correct value. Let's pretend our C# union also defines implicit conversion operators and let's take a ride to wonderland.

    public class NumberHandler:Handler<int|double> 
        protected override int|double  Start(int|double value)
            return Math.Pow(value,2);
        override  override void  Complete(int|double value)
            Console.Out.WriteLine("Got {0}",value);

The compiler has to do some magic casting behind the scenes for this to actually work

I'm spoiled already.

Go get hooked on Ceylon now!

I'm itching to see some of those features in the .Net framework...

No comments:

Post a Comment

Please leave your comments in English or French and I will be pleased to answer them if you have any questions.

Spammers will be walked down the plank matey. Arrr!