New reference type designed to handle templates better

Jonathan Marler via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 7 11:42:56 PST 2014


On Wednesday, 5 November 2014 at 19:27:59 UTC, Jonathan Marler 
wrote:

I haven't gotten any responses on this so I'll try one more time. 
The proposal is to add a new function parameter attribute, called 
"pref" for now.  Here's the difference:

ref : modifies the caller's variable
pref: modifies the CONTENTS of the caller's variable

Here's some examples.

These are all exactly the same:

void modify(int* x)      { *x = 3; }
void modify(ref int x)   { x  = 3; }
void modify(pref int x)  { x  = 3; }
void modify(pref int* x) { x  = 3; }

These are not the same

void modify(class c)      { c.myfield = 3; }

void modify(ref class c)  { c.myfield = 3; }
// NOT EQUIVALENT
// the ref parameter means the function can modify which
// instance the callers variable is actually pointing to.
// This adds an unnecessary level on indirection.

void modify(pref class c) { c.myfield = 3; }

Keep in mind, the pref attribute isn't very useful for 
Non-Template functions because you can just use 'ref' or not. 
However, in a template, the 'pref' attribute will select whether 
the parameter is pass by reference or pass by value based on the 
type.


More information about the Digitalmars-d mailing list