About ref used for performance reasons with struct

Era Scarecrow rtcvb32 at yahoo.com
Mon Feb 11 14:08:43 PST 2013


On Monday, 11 February 2013 at 22:03:52 UTC, Era Scarecrow wrote:
> Output:
>
> 18FDD0- main
> 18FDAC- fun
> is passed by value/copy

  Now had the function been 'void fun(char[] test)', then passing 
a static/fixed array would have errored and you'd be required to 
send a slice of it's contents, which then is by ref. Seems using 
the address doesn't seem to be very helpful, so for slices you'd 
refer to the ptr of the array. I'm sure there's other tests that 
can be done... to help with the expected behavior

   //appended to main
     fun2(test);
     if (test == "test")
       writeln("is passed by value/copy");
     else {
       assert(test == "best", "Something's wrong...");
       writeln("is passed by slice/ref");
     }
   }

   void fun2(char[] test){
     assert(test == "test");
     //&test points to local test not it's contents
     writeln(test.ptr, "- fun2");
     test[0] = 'b';
     assert(test == "best");
   }

new Output:
18FDD0- main
18FDA4- fun
is passed by value/copy
18FDD0- fun2
is passed by slice/ref


More information about the Digitalmars-d mailing list