• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How does the compiler figure out conflicting return types, and why no warnings?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How does the compiler figure out conflicting return types, and why no warnings?


  • Subject: Re: How does the compiler figure out conflicting return types, and why no warnings?
  • From: Alastair Houghton <email@hidden>
  • Date: Wed, 12 Dec 2007 13:02:50 +0000

On 12 Dec 2007, at 12:43, Jonathan del Strother wrote:

I get no warnings no matter what warning switches I turn on, yet the
code fails miserably at runtime : the compiler is expecting
[disclosure rotation] to return a float (presumably from the -[NSEvent
rotation] message), so we get garbage data in the return value.

Obviously there's no way for the compiler to really know what to do
here, since 'disclosure' is of type id, and there's at least two
methods called 'rotation' with differing return types.  But shouldn't
we at least get a warning?

I'm sure they've added a warning for this in Xcode 3. Which version of Xcode/GCC are you using?


And just how does the compiler decide which return type to use?

It guesses (by just picking one of the methods it knows with that selector---I don't think it's any more sophisticated than that). That's all it can do, because it doesn't know the type of object.


This is why you should try to be as specific as possible about the types of your objects. Using "id" indiscriminately can lead to bugs, as you've discovered.

You may then ask, well, what about the case where I have a lot of different objects that all respond to my -rotation method? In such a case, you can either define a protocol, then use e.g.

  id<MyRotationProtocol> obj = [foo getMyObject];
  int bar = [obj rotation];

or add the method to a superclass of your objects, then use the superclass' type:

  MySuperClass *obj = [foo getMyObject;]
  int bar = [obj rotation];

or if only some objects respond to your -rotation message, you might consider

  id obj = [foo getMyObject];
  if ([obj respondsToSelector:@selector(rotation)]) {
    int bar = [(id<MyRotationProtocol>)obj rotation];
  }

or similar. (The type cast only needs to be to an object whose - rotation selector has the same signature, which might seem a bit hacky until you consider how the ObjC runtime actually works.)

Another good solution is to choose a different name for your selector. For instance, it would be unwise to write a -size method that returned something other than an NSSize. I know, because I made that mistake myself once.

You might consider -orientation rather than -rotation, for instance.

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: How does the compiler figure out conflicting return types, and why no warnings?
      • From: "Jonathan del Strother" <email@hidden>
References: 
 >How does the compiler figure out conflicting return types, and why no warnings? (From: "Jonathan del Strother" <email@hidden>)

  • Prev by Date: Re: type checking on enums
  • Next by Date: Re: pragma nowarn
  • Previous by thread: How does the compiler figure out conflicting return types, and why no warnings?
  • Next by thread: Re: How does the compiler figure out conflicting return types, and why no warnings?
  • Index(es):
    • Date
    • Thread