About ref used for performance reasons with struct

Era Scarecrow rtcvb32 at yahoo.com
Mon Feb 11 14:03:51 PST 2013


On Monday, 11 February 2013 at 21:45:11 UTC, Nick Sabalausky 
wrote:
> On Mon, 11 Feb 2013 07:52:28 +0100 "deadalnix" 
> <deadalnix at gmail.com> wrote:
>> 
>> I'm thinking about it for a while now and I'm now convinced 
>> that we should allow the compiler to do that job for us. Let 
>> me explain.
>> 
>
> To be honest, up until recently, I mistakenly thought the 
> compiler DID do this. (Yea, my mistake.)
>
> Is it possible I had confused structs with static arrays? Do 
> static arrays do that?

  I'm pretty sure static/fixed arrays are more like structs, and 
thereby whole copies are made rather than sending a slice. Let's 
find out.

   void main() {
     char[4] test = "test";
     writeln(&test, "- main");
     fun(test);
     if (test == "test")
       writeln("is passed by value/copy");
     else {
       assert(test == "best", "Something's wrong...");
       writeln("is passed by slice/ref");
     }
   }

   void fun(char[4] test){
     assert(test == "test");
     writeln(&test, "- fun");
     test[0] = 'b';
     assert(test == "best");
   }

Output:

18FDD0- main
18FDAC- fun
is passed by value/copy


More information about the Digitalmars-d mailing list