auto ref is on the docket

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 23 04:03:08 PDT 2015


On Tuesday, 23 June 2015 at 06:49:50 UTC, Daniel N wrote:
> On Tuesday, 23 June 2015 at 01:07:17 UTC, Jonathan M Davis 
> wrote:
>> No, you can't guarantee that an auto ref parameter won't be 
>> mutated, but auto ref clearly indicates that you intend to 
>> accept both lvalues and rvalues, meaning that it would be 
>> stupid to be writing the function with the idea that you would 
>> be passing in an argument which would be mutated. ref on the 
>> other hand clearly indicates the intention to mutate the 
>> argument.
>>
>> - Jonathan M Davis
>
> That is why, as a good API designer, you'd express that fact by 
> adding const ontop of it, i.e. const ref.

Which does not generally work in D. It can in some cases, but 
const is so restrictive in D that you simply cannot use it just 
because you don't intend to mutate a variable. Too many types 
won't work with const, and many types _can't_ work with const. 
const has its uses, but you have to be careful with it, and in 
the general case, that means that you can't put it on function 
parameters just to indicate that the function argument is not 
going to mutated.

- Jonathan M Davis



More information about the Digitalmars-d mailing list