Amazon.com Widgets Argument and Return Types with Dynamic Typing
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 10:58:24 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
| | | |-+  Argument and Return Types with Dynamic Typing
Pages: [1]   Go Down
Print
Author Topic: Argument and Return Types with Dynamic Typing  (Read 1437 times)
imparare
Newbie
*
Posts: 14






« on: April 21, 2010, 03:35:26 AM »

Hi,

I am a bit unsure on what is being explained on p195.

As far as I can tell if you have:

result = [dataValue1 add: dataValue2]; //dataValue1 & 2 are id types

Then this is fine as the types in the two id's will be checked at runtime and the correct method will be selected. What I don't understand is the part about if you have two methods and their arguments are different (say an object and an integer) then the complier may generate incorrect code?

I have several questions but when I just typed them into here they didn't make a whole heap of sense, so a more genera explanation of this would be great or a pointer to somewhere else in the book where it is explained further.

thank you

Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: April 21, 2010, 05:21:31 AM »

When your program is built, the compiler will look for all the add: methods it knows about based on the classes you are using.  If it finds two add: methods with inconsistent argument or return types (say one that takes a float and another that takes an object as its argument), it will issue an error message..  This only happens if you're sending a message to an object whose class is not statically-typed (i.e., an id object).

SInce the class of the object isn't known until runtime, the compiler doesn't know how to generate the right code to call the method, so it issues an error message.

Hope that clears it up.  It's a little complicated to explain.  Smiley

Cheers,

Steve



Logged
imparare
Newbie
*
Posts: 14






« Reply #2 on: April 21, 2010, 09:06:49 AM »

Thanks Steve.

It issues the error message at compile time as it knows that it will get confused at runtime ?

Why can't it not issue an error message and just look inside id at runtime and then say: 'ok it's a float so it's the method with a float as an argument' ? Is it because it would then have to check each class (at runtime) to see which one was the float whereas if it was a custom type (Fraction or Complex for example) it knows that it should go to that class for that method because that is the type ?


thanks again.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: April 21, 2010, 10:32:09 AM »

The compiler generates code to setup the call to the method (this ends up being a function call).  This typically involves pushing the arguments onto the stack (let's forget about optimizations here).   It also generates code to handle the value returned by the method.  This is a compile-time process.  At runtime, the class of the receiver is tested and the appropriate method selected.  However, all the other code for calling the method and handling its return value has been previously-generated.  Therefore, the argument types and the return value type must be consistent across all possible methods that may be called at runtime.

Cheers,

Steve Kochan
Logged
imparare
Newbie
*
Posts: 14






« Reply #4 on: April 22, 2010, 05:33:37 AM »

Thank you Steve. I can't say I have completely grasped it but I have been working on some examples but clearly I am not that proficient yet that I can completely follow it. As it is slowing me down and given that I would get a compile error then hopefully track it down then for now I am not worried. As I carry on through the book and gain more experience I will return to it.

one final question on this. Would this generate a compile time error or warning ?

thank you again
« Last Edit: April 22, 2010, 05:41:06 AM by imparare » Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #5 on: April 22, 2010, 10:02:41 AM »

If it finds two methods with inconsistencies, it will generate an error (although it seems to be able to handle some inconsistencies such as one method taking a float and another an int).

Cheers,

Steve Kochan
Logged
imparare
Newbie
*
Posts: 14






« Reply #6 on: April 22, 2010, 10:42:49 AM »

And as luck would have it I made an example with (yep you guessed it) and int and a float and I could not get it to fail.

Will make an example with an object and an int now.

thank you
Logged
subbulu
Newbie
*
Posts: 1






« Reply #7 on: August 21, 2011, 08:47:53 AM »

Let me put what I understand based on the text in the book and all the messages in this thread.

Please correct me if I am wrong.

After the compilation is done, the resulting code would need types of all the arguments and return variable. At this time  there still could be multiple functions that are possible if so run time will choose the right one.

To put this in a different way, if more than one class has same name for a function, all such functions should match on  type for all the arguments and return type, otherwise compiler will generate an error.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.