references and function calls

Derek Parnell derek at psych.ward
Fri Mar 16 05:17:08 PDT 2007


On Fri, 16 Mar 2007 06:54:00 -0400, Neal Becker wrote:

> I've been reading the D specification.  A couple of questions:
> 1. Do I understand correctly that D has no reference type declarations
> (there is no e.b., int& x;)?

That is correct. There are alternative forms for the various uses for
references to data. For example, the use of inout and out in function
signatures.

> 2. Reading the section on functions, unfortunately I don't think it is clear
> on how arguments are passed.  Do I understand correctly that argument
> passing is like java: POD are passed by value but other objects (class
> instances) are passed by reference?

Class objects and arrays are passed use a reference mechanism.
POD, is passed as copied data if the argument is an 'in' type otherwise it
is passed by reference.

  void funcOne(in int A, inout int B, out int C)
  {
       if (A > 0)  // The value is readable.
          A = 1;   // The value is changable but 'in' prevents changes
                   // going back to the caller.

       if (B > 0)  // The value is readable.
          B = 2;   // The value is changable and changes
                   // go back to the caller.

       if (C > 0)  // The value is not readable as 'out' always
                   // initializes it before the function gets control.
          C = 3;   // The value is changable and changes
                   // go back to the caller.
  }

  int a = 7;
  int b = 8;
  int c = 9;
  funcOne( a,b,c)
  // Now a is still 7 ('in' prevented changes.)
  //     b is 2  (change got back to me)
  //     c is still 9 ('out' prevented called func from seeing
  //                    its value at call time)


  void funcTwo(in char[] A, inout char[] B, out char[] C)
  {
       // NB: When passing arrays, the value passed is the reference!

       if (A.length > 0) // The value is readable.
          A = "a".dup;   // The value is changable but 'in' 
                         //prevents changes
                         // going back to the caller.

       if (B.length > 0)  // The value is readable.
          B = "b".dup;   // The value is changable and changes
                   // go back to the caller.

       if (C.length > 0)  // The value is not readable as 'out' always
                   // initializes it before the function gets control.
          C = "c".dup;   // The value is changable and changes
                   // go back to the caller.
  }

  char[] aa = "aaa".dup;
  char[] bb = "bbb".dup;
  char[] cc = "ccc".dup;
  funcTwo( a,b,c)
  // Now aa is still "aaa" ('in' prevented changes.)
  //     bb is "b"  (change got back to me)
  //     cc is still "ccc" ('out' prevented called func from seeing
  //                    its value at call time)


Also note that passing arrays as 'in' or 'inout' both permit the called
function to change the data referenced by the parameter. 

   void funcThree(in char[] a)
   {
       if (a.length > 0)
          a[0] = 'A'; // 'in' only prevents changes to 'a' and
                      // not changes to what 'a' refers to.
                      // Thus changes I make to the .ptr or .length
                      // properties of 'a' are not sent back to the
                      // caller.
   }
   char[] A = "321".dup;
   funcThree(A);
   // The data in the array is now "A21"

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list