discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is this casting needed?


From: Helge Hess
Subject: Re: Why is this casting needed?
Date: Sat, 25 Mar 2006 13:18:09 +0100

On 25. Mrz 2006, at 12:51 Uhr, Günther Noack wrote:
Am 25.03.2006 um 00:57 schrieb Adrian Robert:
But it's only a warning -- it still works at runtime in either case. What is the compiler doing differently with the cast? Is it some performance optimization?
The resulting code doesn't differ.

If you have those methods:

  @implementation C:
  - (void)addNumber:(float)value;

  @implementation A:
  - (void)addNumber:(short)value;

  @implementation B:
  - (void)addNumber:(NSNumber *)value;

The generated code of course _does_ differ. How exactly it differs depends on the C ABI of the host system.

This is why its recommended to explicitly include the type in the method for base type stuff, like:
  - (unsigned int)unsignedIntValue;
  - (id)initWithUnsignedInt:(unsigned int)value;
etc.


The problem is a bit less when both methods take objects as arguments, like:

  @implementation C:
  - (void)addNumber:(NSNumber *)value;

  @implementation A:
  - (void)addNumber:(NSValue *)value;

Of course in this case the generated code is the same. However, the static type checking being applied is the same in the C compiler. And the reason why the compiler needs to do type checking is also the same.

If you want to accept arbitary objects with your selector, use just
  - (void)addNumber:(id)value;

Which is the 'traditional' ObjC way :-)

Greets,
  Helge
--
http://docs.opengroupware.org/Members/helge/
OpenGroupware.org





reply via email to

[Prev in Thread] Current Thread [Next in Thread]