New reference type designed to handle templates better

via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 7 13:44:50 PST 2014


On Friday, 7 November 2014 at 19:42:58 UTC, Jonathan Marler wrote:
> 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.

I think Manu and deadlnix should comment on this - they 
complained about `ref` here:
http://forum.dlang.org/thread/etjuormplgfbomwdrurp@forum.dlang.org?page=5

I agree that this is a real problem - but the solution you 
propose feels a little ad hoc. It would "solve" this particular 
problem, but what is the deeper cause for the problems with 
current ref?


More information about the Digitalmars-d mailing list