On Sat, Sep 26, 2009 at 4:18 PM, Jeremie Pelletier <jerem...@gmail.com> wrote:
>> type Event = Mouse | Key | Move; > > This can be confusing, for example the first thing that comes to mind for me > is that Event is the bitwise OR result of 3 constants, not an enumerated > type. > > Besides, how is it any different than: > > enum { Mouse, Key, Move }; It's not an enumerated constant. Mouse, Key, and Move are all types, and Event is a discriminated union of the three. See more below. > match(event) is no different than switch(event), except that pattern > matching often implies runtime semantics and is often slower than a straight > jump table generated from a switch. Not in this case. See, when you would do "type Event = Mouse | Key | Move", it's actually more like doing: struct Event { enum Type { Mouse, Key, Move } Type type; union { Mouse m; Key k; Move v; } } Then, when you do "match(e) { Mouse m => ... }" it's actually being turned into: switch(e.type) { case Event.Type.Mouse: alias e.m m; ... case Event.Type.Key: alias e.k k; ... } Basically discriminated unions get rid of this annoying boilerplate that you have to use every time you want a tagged union.