4 ms·
Your ShapeHandler example above is an example of multiple dispatch[1] - something not very fashionable anymore, but which I'd love to see in a statically typed
by lacampbell 10y ago
Your ShapeHandler example above is an example of multiple dispatch[1] - something not very fashionable anymore, but which I'd love to see in a statically typed language. It's a more natural feature for an OO language - but I wonder about the trade offs it has with pattern matching and Algebraic Data Types.
[1] http://wiki.c2.com/?MultipleDispatch http://wiki.c2.com/?MultipleDispatch
- MaulingMonkey 10y agoFWIW you can abuse dynamic in C# to get multiple dispatch, although not with much static checking. using System; class Foo { } class Bar { } static class Program { static void Example(Foo l, Foo r) { Console.WriteLine("Foo Foo"); } static void Example(Foo l, Bar r) { Console.WriteLine("Foo Bar"); } static void Example(Bar l, Foo r) { Console.WriteLine("Bar Foo"); } static void Example(Bar l, Bar r) { Console.WriteLine("Bar Bar"); } static object Create(string arg) { return arg == "Foo" ? (object)new Foo() : new Bar(); } static void Main(string[] args) { dynamic l = Create(args.Length > 0 ? args[0] : "Foo"); dynamic r = Create(args.Length > 1 ? args[1] : "Bar"); Example(l, r); } }