difference between 'id' and 'Object'

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 05:15:27



Sometimes the issue comes up what the difference is between Object
and 'id'.

        id x = Object;

In the above statement 'Object' is the right hand value (in an assignment)

Object is a value. You can assign it to variables.

'id' is a C type.  You cannot assign it to a variable.

 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 05:20:35



> Sometimes the issue comes up what the difference is between Object
> and 'id'.

>    id x = Object;

> In the above statement 'Object' is the right hand value (in an assignment)

> Object is a value. You can assign it to variables.

> 'id' is a C type.  You cannot assign it to a variable.

But what if I want to constrain 'x' to a certain class?

 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 05:26:17




>>       id x = Object;

> But what if I want to constrain 'x' to a certain class?

 To which class would you want to constrain Object ?

 You can test with -isKindOf: or -isMemberOf:, which is the *dynamic*
nature of the Objective-C system.

 It is actually a pity that these essential messages were renamed

        isKindOf: --->  isKindOfClass:

which is a silly change much like perform: -> performSelector:

 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 05:34:50



>  To which class would you want to constrain Object ?

In the example I gave you, I wanted to constrain an object to Sets; I
could, however, have given it a String value.

Quote:

>  You can test with -isKindOf: or -isMemberOf:, which is the *dynamic*
> nature of the Objective-C system.

But that requires much work.  It is much easier to do this:

- (void)foo:(Set *)bar
{
     [bar doStuff];

Quote:}

than:

- (void)foo:(id)bar
{
     if ([bar isKindOf:Set])
         [bar doStuff];
     else
         // insert error handler

Quote:}

...similarly to the difference between real exceptions and the C
'errno' stuff, in that often the programmer will forget to check their
code for errors manually, as it is rather irritating to have to do that.

Quote:

>  It is actually a pity that these essential messages were renamed

>    isKindOf: --->  isKindOfClass:

> which is a silly change much like perform: -> performSelector:

Many of us that are arguing against you have given you the reason why
isKindOf:, isMemberOf:, and perform: have been replaced in NSObject --
though *not* replaced in Object -- with isKindOfClass:,
isMemberOfClass:, and performSelector:, respectively.  You, however,
have chosen to ignore these reasons.  Perhaps we wouldn't need to argue
with you to get ideas into your head if you actually read the substance
of what we have written.
 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 05:41:59



> In the example I gave you, I wanted to constrain an object to Sets; I
> could, however, have given it a String value.

The paradox is the following :

        id a = Object;

is what we want (and what works with POC and Stepstone).

"Object" is an instance so we can assign it to 'id a'.

However, suppose you'd have a syntax like :

        Object a = Object;

Then why would the following not work :

        int a = int;

 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 05:50:15



> The paradox is the following :

>    id a = Object;

> is what we want (and what works with POC and Stepstone).

> "Object" is an instance so we can assign it to 'id a'.

> However, suppose you'd have a syntax like :

>    Object a = Object;

> Then why would the following not work :

>    int a = int;

That's entirely different from my question.  My question was about how
to constrain instances to a certain type.  In your example, the correct
code would be:

Class a = Object;

...but that still has nothing to do with my question.

I wanted to know how to constrain an instance to a certain type.  I
wanted to make sure 's' was always a Set, but I can't do so cleanly with
your code; I can, however, with the Objective-C that is widely in use
today:

Set *s = [Set new];

 
 
 

difference between 'id' and 'Object'

Post by John C. Randolp » Thu, 28 Nov 2002 06:25:09




> > Sometimes the issue comes up what the difference is between Object
> > and 'id'.

> >       id x = Object;

> > In the above statement 'Object' is the right hand value (in an assignment)

> > Object is a value. You can assign it to variables.

> > 'id' is a C type.  You cannot assign it to a variable.

> But what if I want to constrain 'x' to a certain class?

Then you use <classname>* instead of id, eg:

NSWindow *myWindow;  // pointer to a window

id <foo> bar; // bar is a pointer to an object that conforms to the
"foo" protocol

- myMethodThatTakesAnyObject: anyObject
andASpecificKindOfObject:(MySpecificClass *) objectOfASpecificType;  //
anyObject is implicitly typed as "id", and objectOfASpecificType is a
pointer to an instance of MySpecificClass.

-jcr

 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 06:44:33



> Then you use <classname>* instead of id, eg:

> NSWindow *myWindow;  // pointer to a window

Yes, but if you go by what he says, you can only declare instances to be
of type 'id.'

Quote:

> id <foo> bar; // bar is a pointer to an object that conforms to the
> "foo" protocol

I have to define a new protocol in that case, but it seems as though I
should just be able to declare the class.

Quote:> - myMethodThatTakesAnyObject: anyObject
> andASpecificKindOfObject:(MySpecificClass *) objectOfASpecificType;  //
> anyObject is implicitly typed as "id", and objectOfASpecificType is a
> pointer to an instance of MySpecificClass.

See first response.

Quote:

> -jcr

(I know perfectly well how to do this stuff; I was just making a point
against what he claims classes are in Objective-C)
 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 06:45:27



Quote:

> id <foo> bar; // bar is a pointer to an object that conforms to the
> "foo" protocol

No, no, protocols aren't a well-designed addition.

Protocols are just an attempt to turn Objective-C into a statically typed
system.  

There is no need to do that.

 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 06:51:39



> No, no, protocols aren't a well-designed addition.

> Protocols are just an attempt to turn Objective-C into a statically typed
> system.  

> There is no need to do that.

But if you have no protocols, and you can only declare variables to be
of type 'id,' then how *do* you constrain variables to one class?

Furthermore, *C* is a statically typed language, and since Objective-C
is a superset of C, Objective-C is therefore a statically typed
language.  You must be thinking of Smalltalk.  Then that would explain
why you think that variables whose values are instances can only be of
the 'id' type.

 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 06:52:39



> You must be thinking of Smalltalk.

I am.

Quote:> Then that would explain
> why you think that variables whose values are instances can only be of
> the 'id' type.

It does.

It also explains our reasoning behind Blocks

(see http://users.pandora.be/stes/block98.html)

 
 
 

difference between 'id' and 'Object'

Post by Michael J As » Thu, 28 Nov 2002 06:59:45




> > You must be thinking of Smalltalk.

> I am.

Funny, I must have wandered off from comp.lang.objective-c. And I could
have sworn that you write POC, and that POC is an Objective-C compiler. Am
I mistaken? Does it really compile smalltalk?

--
"From now on, we live in a world where man has walked on the moon.
 And it's not a miracle, we just decided to go." -- Jim Lovell


 
 
 

difference between 'id' and 'Object'

Post by Taylor Campbel » Thu, 28 Nov 2002 07:02:25


Then why don't you use Smalltalk?  Objective-C *is*, after all, a
different language.
 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 07:38:48



Quote:

> Funny, I must have wandered off from comp.lang.objective-c. And I could
> have sworn that you write POC, and that POC is an Objective-C compiler. Am
> I mistaken? Does it really compile smalltalk?

 Objective-C is a system in the family of languages like SELF or Smalltalk.
 
 
 

difference between 'id' and 'Object'

Post by David Ste » Thu, 28 Nov 2002 07:42:48



> Then why don't you use Smalltalk?  Objective-C *is*, after all, a
> different language.

 In the case of UTC Bar Chart, I had a Perl program that generated code.

 But several hundreds (or even thousands) of these charts were generated,
from within Perl.  Making an Objective-C binary, sped up the entire 'batch' process (ran from CRONTAB) a lot.

 This works fine.  Perl can easily invoke the 'utcbarchart' utility.

 Objective-C integrates smoothly in the C/Unix/Perl environment, while
Smalltalk images do not have this emphasis on integration with C/Unix.