Thursday, January 17, 2008

Extension Methods vs. Traits & Implicit Conversions (a Scala question)

I personally prefer the idea of extension methods (most obviously in C#) vs. Scala's use of both traits/mixins and implicit conversions to achieve the same result. Simplifying the argument for each side, I find this:
  • Extension methods: Simpler, less bloat, more predictable, extensible by third parties.
  • Mixins: More flexible (since classes can override). Designer control for uniformity, but less extensible. Bloats compiled code. More complicated than static extension methods.
  • Implicit conversions: Extensible but results in more complicated semantics. (I understand that C# also has implicit conversion, but it's not meant for this use case to my understanding.)
So, Scala folks, why should I prefer mixins/implicits (the Scala way), and how do you decide how much to mix in and how much to extend by implicits?

My own idea is something I call a "role" which is a combination of a runtime-erased type alias with optional extension methods. But I won't go in depth right now.

2 comments:

  1. Have you realized how short-sighted this post was yet?

    Even C# people who have never heard of Scala can tell you that the weaknesses of extension methods are severe and potentially dangerous, and readily apparent once you get over the initial "Cool!" factor of Enumerable and actually start rolling your own.

    ReplyDelete
  2. I don't see how extension methods are much different than static overloading, such as what Java and C++ provide, but I haven't spent enough time in C# to know for sure. Do you have any references on the matter that I can review?

    However, I've lately become more a fan of arbitrary multiple dynamic dispatch, ignoring OOP entirely. And error out for multiple matches. Well, that's in theory, since I haven't used such a system in practice, although I know they exist.

    ReplyDelete