FROM : Damien Sorresso
DATE : Sat Jul 08 03:00:31 2006
On 7 Jul, 2006, at 7:29 PM, Rob Ross wrote:
> I'm just learning Obj-C myself, so maybe my understanding is not
> correct. But, don't you actually have the option to use static
> typing in certain instances, and use dynamic typing in others?
>
> Eg:
>
> id myStr;
>
> vs
>
> NSString *myStr;
>
>
> Won't the second version give you the benefits of static typing
> (compiler time checking)?
>
Well, kinda sorta. The compiler will check, but the messages are
still bound at runtime. For example, this statement won't even
generate a warning.
NSString *str = [[NSData alloc] init];
because both `NSData' and `NSString' have `init' methods, and `init'
methods return an object of type `id', which is NOT statically typed.
For more egregious violations, like for example ...
Cat *cat = [[Cat alloc] init];
[cat bark];
... the compiler will throw up a warning and tell you that `cat' may
not respond to the selector `bark'.
> So my understanding is you can have the best of both worlds. Or
> have I misunderstood something?
>
Not quite. Objective-C is pretty well entrenched in the runtime.
You have to remember that you're NOT actually "calling a method" when
working with Objective-C objects. You are sending them messages.
Think about it this way. If language objects are people, then C++
objects are basically puppets. You don't tell them to do something;
you just pull the string, and they do it. Objective-C objects are
more independent. You TELL them that you'd like something done, and
they'll do it if they can.
--
Damien Sorresso
"That is the saving grace of humor, if you fail no one is laughing at
you."
-A. Whitney Brown
DATE : Sat Jul 08 03:00:31 2006
On 7 Jul, 2006, at 7:29 PM, Rob Ross wrote:
> I'm just learning Obj-C myself, so maybe my understanding is not
> correct. But, don't you actually have the option to use static
> typing in certain instances, and use dynamic typing in others?
>
> Eg:
>
> id myStr;
>
> vs
>
> NSString *myStr;
>
>
> Won't the second version give you the benefits of static typing
> (compiler time checking)?
>
Well, kinda sorta. The compiler will check, but the messages are
still bound at runtime. For example, this statement won't even
generate a warning.
NSString *str = [[NSData alloc] init];
because both `NSData' and `NSString' have `init' methods, and `init'
methods return an object of type `id', which is NOT statically typed.
For more egregious violations, like for example ...
Cat *cat = [[Cat alloc] init];
[cat bark];
... the compiler will throw up a warning and tell you that `cat' may
not respond to the selector `bark'.
> So my understanding is you can have the best of both worlds. Or
> have I misunderstood something?
>
Not quite. Objective-C is pretty well entrenched in the runtime.
You have to remember that you're NOT actually "calling a method" when
working with Objective-C objects. You are sending them messages.
Think about it this way. If language objects are people, then C++
objects are basically puppets. You don't tell them to do something;
you just pull the string, and they do it. Objective-C objects are
more independent. You TELL them that you'd like something done, and
they'll do it if they can.
--
Damien Sorresso
"That is the saving grace of humor, if you fail no one is laughing at
you."
-A. Whitney Brown






Cocoa mail archive

