• 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: Bug in GCC 3.1?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug in GCC 3.1?


  • Subject: Re: Bug in GCC 3.1?
  • From: Thomas Lachand-Robert <email@hidden>
  • Date: Mon, 6 May 2002 23:25:49 +0200

Le lundi 6 mai 2002, ` 06:44 , email@hidden a icrit :

Thomas Lachand-Robert wrote:
|I have found a problem that look like a bug in GCC 3.1, but maybe I just
|don't understand something.
|If you write in an Obj-C class:
|
|-(void) bar:(id<NSMutableCopying>)t { /*whatever */; }
|
|-(void) foo:(id<NSCopying>)t {
| [self bar:t];
|}
|then the compiler will complain in foo as follows:
|warning: object does not conform to the `NSMutableCopying' protocol

No, the compiler is behaving as it should. Declaring an argument as "id" does *not* mean to suppress type checking. It means merely that the argument a pointer to an Objective-C object whose exact type is unspecified.
That's what I meant. This doesn't explain why [self bar:(id)t] doesn't work, since NOW the object type is unspecified as you say.

Declaring the argument "id<NSCopying>" says that it's a pointer to an Objective-C object which implements the NSCopying protocol. That is, the object *will* respond to any of the selectors defined by the NSCopying protocol. Declaring the argument "id<NSCopying>" is more specific than declaring it merely "id".
However, you've told the compiler that "bar:" requires that its argument implement the NSMutableCopying protocol, not the NSCopying protocol. Since "foo:"s argument implements NSCopying, but not NSMutableCopying, the compiler properly complains.
If you try to cast the argument to id<NSMutableCopying>, you're just writing explicitly what the compiler's doing anyway when it calls "bar:", so nothing has really changed.

But the cast doesn't work, either. I get the same error message, though for any other type, using an explicit cast shut up the compiler.

|Also if you change the declaration of bar as follows:
|
|-(void) bar:(NSObject<NSMutableCopying>*)t
|
|the compiler does NOT complain in all three cases! This means that
|NSObject<NSMutableCopying>* is considered MORE general than
|id<NSMutableCopying>...

No, it doesn't mean that it's more general. It means only that the compiler believes that the various protocols *are* implemented by the objects in question.
These protocols are not implemented by this type, and I also tried with different protocols.

I confess, I'd expect an error message in this case even so. Perhaps more expert minds than mine can explain this one. (The best place to find such minds will be on the cocoa-dev mailing list.)

That's why I cross-post to cocoa now.

Overall, it looks like you could benefit by reading more about how protocols fit in with Objective-C.

Glen Fisher

I have read almost everything existing about protocols (that's not a lot), and still don't understand what happens here. However, you look as confused as I am, so... Thanks to have tried to help, anyway.

Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Bug in GCC 3.1?
      • From: email@hidden
  • Prev by Date: [OT] Default Paper Size
  • Next by Date: Re: [OT] Default Paper Size
  • Previous by thread: Re: [OT] Default Paper Size
  • Next by thread: Re: Bug in GCC 3.1?
  • Index(es):
    • Date
    • Thread