Hacker News new | past | comments | ask | show | jobs | submit login

Out of the myriad new languages the crop out almost daily, Dart stands out as clean, modern, fast and ready to roll with a large standard library. The team is strong and stacked with experts in the field.

On the flip side, algebraic data types have been invented in the 70es and are fundamental to the logic of programming. I don't understand the process through which an otherwise informed team ships half-baked sum types in 2014 in the guise of enums.




Sum types and subclasses cover much of the same problem space just with different trade-offs. Sum types give you exhaustiveness checking, while subclasses give you open-ended extensibility.

Given that Dart already has subclasses and they're the natural way for people to solve most problems where you'd reach for a sum type in other languages, I don't think there's as much need for enums to be larger and feature-rich. Instead, the language team focused them on the narrow use cases of wanting just an exhaustively checked numeric subrange, similar to enums in C#.

I do personally wish you could add methods and fields to them (á la Java but without case-specific methods and fields), but I think the language team wanted something easier to compile to a bare number in JS. Maybe they'll get added later.


Unless I'm missing something, enums are closed, thus overlap with sum types for the use-cases. Subclasses are an implementation detail in this context. Here is an example that uses subclasses to implement generalized enums, aka sum types:

  enum List {
    Cons(head, tail) {...}
    Nil {...}

    int length() {
      switch (this) {
        case Cons(head, tail): return 1 + length(tail) 
        case Nil: return 0
      }
    }
  }
Mind you, this is syntactic sugar and you can do it by hand with enums + sublcasses. But the whole point of clean languages is to remove unnecessary boilerplate :)


Haxe has another example of this: http://haxe.org/manual/types-enum-instance.html

Note that enums in Haxe are pure data types, and can't have member functions and/or values. However, since enums are public by nature, it's possible to achieve this easily with "using": http://try.haxe.org/#87bF5


They're experts alright but it would appear that although you can take the person out of the Smalltalk, you can't quite take all the Smalltalk out of the person (not a bad thing in my opinion).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: