• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how return value is resolved

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am not able to understand how does the compiler knows the return type, as here is only assignment.
 
Henry Wong
author
Marshal
Pie
Posts: 21225
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An assignment has a value. And that value is the variable being assigned. With this, these expression...

c = new C();
c2 = c;

can be chained to this...

c2 = c = new C();


Originally posted by vijay shanker:


I am not able to understand how does the compiler knows the return type, as here is only assignment.


For your code, if c is equal to null, the variable is assigned, but the ternary operator then returns the value of the variable that has just been assigned.

Henry
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit OT here, and I'm probably taking a glib example and blowing it out of proportion...

I understand the ternary operator alright, and how the method returns either a new C reference or the original reference, but what if the c field isn't an instanceof C? Shouldn't there be a check for that?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Lang:

I understand the ternary operator alright, and how the method returns either a new C reference or the original reference, but what if the c field isn't an instanceof C? Shouldn't there be a check for that?


Given that the return type is C, and "new C()" is assigned to c, this code will not compile unless c is declared as being of type "C". So there is such a check; it's done when this code is compiled. It would impossible for c to contain an object which wasn't type-compatible with C at runtime, so once it's compiled, there's no way the code can fail.
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
this code will not compile unless c is declared as being of type "C".


Thank you, that's just what I was looking for. I guess I was thinking it would be possible to change the reference at run time, but it's already set in bytecode and can't change.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, for your replies, i was not aware of that behavior of this operator.

actually c is an instance variable of type C in a class. So,.......
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay,
the operator does not have that effect,

the variable c has to be defined somewhere,
and it has to be C because it's compiling,
so we're assuming it's a instance variable or global typed C,



there could be a superclass (B) or interface (Clike) and it would be a valid assignment statement


however, for the
return c;
to be valid, the variable must be defined as a C or subclass of C,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic