function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jim CripeJim Cripe 

Functon Parameters with Objects - Call by Reference Issue in Winter '09?

I may have come across another SF bug or at least a documentation hole.  I turned in a case, which I’m told was attached to an existing case.

 

If you have run this code, the assertion will fail in Winter '09:

 

global class TestCallByReferenceClass{

 

public static testMethod void testFunction(){

    Contact con = new Contact();

    System.debug('testFunction- con before: ' + con);

    Function1(con);

    System.debug('testFunction- con after: ' + con);

    System.assertEquals('TestLast',con.LastName); //Assertion will fail!

}

 

public static void Function1(Contact con){

    System.debug('Function1- con before: ' + con);

    con = new Contact();

    con.LastName='TestLast';

    System.debug('Function1- con after: ' + con);

}

}


To explain what happens in Apex, I need to give a couple of definitions:  "Call by value" means that a function actually gets a copy of a variable to use inside the function.  "Call by reference" means that the variable passed into a function is the variable itself.  Call by reference should make changes to a parameter variable inside the function be in the parameter variable after the function call ends because the variable outside and inside the called function are one and the same.


Apex is supposed to pass all objects by value, but there is a bit of a twist.


It turns out that the Contact "con" parameter in the code above is created as a reference variable to the real object and that reference variable is passed using call by value into the function.

 

The affect of this is that the "con" reference parameter that the function starts executing with is reassigned to hold a reference to the new object and forgets about the original object it was referencing.  When the function returns, the outer "con" object is still there untouched, but the "con" object with its LastName change went away with the copy of the reference variable that had existence only inside the function.


If you remark out the "con = new Contact();" line in Function1, the assertion does not fail because there is no reference variable reassignment.  The outer Contact "con" hooks to the same "con" inside the function so the changed LastName value is available when the function finishes.

 

This is the way Java behaves and I understand SF is programmed in Java.

 

SalesForce needs to document clearly if Apex behaves properly in this case, or fix Apex so that the function parameters to objects pass newly created objects out to be available when the function exits.




Message Edited by Jim Cripe on 01-15-2009 09:19 PM
SuperfellSuperfell
java, c#, objective-c, c++, vb.net all work like this, in fact i'm struggling to think of an OOO language that doesn't right now. Can you point to the part of the docs that creates the confusion? we'll try and get it clearer.
Jim CripeJim Cripe
Hi Simon,

Your response to my posting is right to my point.  I couldn't find any explanation of this behavior and I am asking SalesForce to make it clear in their documentation, making it accessible when searched for.

Try going to the "Web Services Developer's Guide" link in your message and searching for some terms like "object parameter", "call by reference", "object function parameter", ....  You get a lot of results, but how to determine which result might cover this issue is impossible.

This is core Apex behavior and instead of the "Web Services Developer's Guide" that you referenced, the more appropriate document would be the "Apex Language Reference", (available at http://wiki.apexdevnet.com/index.php/Documentation.)  Searching it using combinations of appropriate terms still doesn't find  information on this subject, and the referenced Java tutorial at Sun on object oriented concepts doesn't touch on this either.

Maybe in the "Apex Language Reference", in the "Classes, Objects, and Interfaces" section could include an explanation of this concept identified with a heading like "Object References", explaining the "call by reference" concepts similar to the way I did?
SuperfellSuperfell
the link to the api docs is my standard signature, and was not in reference to your question. So, just so i'm clear, you'd like the docs to explicitly call this out, its not that there's something existing in the docs that lead you to believe it works some other way?
Jim CripeJim Cripe
There was nothing in the documentation I could find explaining how objects should behave in Apex with call by reference.

When I queried our premier support about what I experienced with a case (#02359936), the case got closed.  There was no information in the case about why it was closed.  The "Solutions" or "Related Comments" had nothing.   I got no feedback of any kind by other means.  (Seems like an email to the submitter would be the friendly thing to do when cases are updated too.)

When I asked our support guy what happened, the response was:
"here at salesforce.com we use a different object to report on known issues within the database. When we get a case we confirm the issue and then escalate it using that object. With all the data, I have attached the data that you sent me last night on the object. Then we close the case."
The way I interpreted that response was there was an open case somewhere in the SaleForce's internal issue tracking system concerning call by reference with objects.

Maybe when SalesForce closes out a ticket for the reasons given me, their "different object" should have a some text explaining the situation that can be pasted into the case as they close it?  Kind of a "resolution note"?


esaesa

Hi Jim,

 

Thank you for taking the time to post your findings.  I agree that the documentation does not define how it handles objects as function parameters since I looked through the most current APEX docs up and down and then tried different search combinations to find the answer.  I came to this thread after I couldn't find it.

 

Thank you again!  Now I know the answer to my question and hopefully SF.com will add this information  to their documentation.  I wanted to chime in to hopefully help push the issue.  This is basic information developers need to know.

 

 -eileen 

Message Edited by esa on 01-30-2009 09:37 AM
Message Edited by esa on 01-30-2009 09:37 AM