|
 |
On 10/22/2010 12:33 PM, Warp wrote:
> Darren New<dne### [at] san rr com> wrote:
>> http://journal.stuffwithstuff.com/2010/10/21/the-language-i-wish-go-was/
>
> I'm not so sure I agree with the necessity of 'union'.
>
> 'union' in C is basically a construct which exists to save some bytes
> of memory in some rare circumstances. The price you pay for it is reduced
> type safety: You always have to make sure you are using the object with
> the correct type, or else you'll get garbage. More often than not, unions
> are not used raw, but embedded in structs, where one of the struct members
> tells the type of the union. While in some situations you can use this to
> save memory (especially if you need to instantiate this struct millions of
> times), it introduces the overhead of the type member and having to always
> check it. (Without the union there would be no need to write any conditionals
> based on the type.)
>
> You could have the language always automatically check the proper type
> of the union, but then you'll be having overhead on every single operation
> you are doing with that union (overhead which could in many cases be avoided).
> It would be rare for the memory saving advantage to be worth the overhead.
There might be an application in which a set of classes derived from a
common ancestor need to be forced to have the same size. Then a member
which is of a different type in different classes would best be a union.
Class member functions (late bound) would know which member of the
union to access without having to type check.
Really, though, the union is a kludge built into C for the resource-poor
early days of computing.
Regards,
John
Post a reply to this message
|
 |