|
![](/i/fill.gif) |
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.
As for removing null pointers, that would present problems of its own.
Null pointers in languages like C, C++ and Java are often used for useful
things. For example if you have a linked list, a null 'next' pointer is
a good indication of the element being the last one in the list. Without
the possibility of having null pointers you would have to device some other
kind of solution.
In Objective-C you usually don't have to check for null pointers. You can
(usually) safely make method calls on objects even if the pointer is null
(in this case no method call will be done, and if the method has a return
value, the default value will be returned). This way you can do all kinds
of things without having to worry about null pointers. (Of course this
introduced a slight overhead on every method call, but method calls in
Objective-C are really heavy regardless, so the small null-pointer check
doesn't add significantly).
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |