The difference being that your Pascal example has no notion of one side being the real side, hence it being useful for subverting the type system, and an Either has that defined and doesn't allow any subversion, but lets you abstract over it being one thing or the other. In C it can be used for 'evil' too but a common approach is to have a tag saying what the type really is.
On Tue, Oct 30, 2012 at 11:25 PM, Cédric Beust ♔ <[email protected]> wrote: > Bouncing off another topic mentioned in the latest podcast: union types. > Yes, C had them but the first time I was exposed to them was in Pascal, and > the discussion on the podcast reminded me of an awesome hack that blew my > mind a very long time ago. > > Pascal was well known to be very strict and safe. Among other things, it > didn't let you access the memory directly, which was a big deal in the 8 bit > era where memory protection was a distant dream and PEEK and POKE were how > you wrote games. > > And then, one day, somebody found a way to address the memory using standard > Pascal. Here is the trick (from memory, so it's probably not quite correct): > > type > b = record > x : array[1..65536] of ^integer; > y : integer; > end; > > This declares a union type that is made of either an array of 65k pointers > to integers or a single integer. Then you initialize this record in its "x" > side with the memory address you want to peek and you access it by using the > "x" side of the record. > > It took me months to understand what this code did, but what a revelation it > was when it finally clicked... > > By the way, these types are formally known as "sum types" (Either is one of > them). > > -- > Cédric > > > -- > You received this message because you are subscribed to the Google Groups > "Java Posse" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en. -- You received this message because you are subscribed to the Google Groups "Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
