Call site 'ref'

Alex Rønne Petersen xtzgzorex at gmail.com
Sun Jan 15 23:31:01 PST 2012


On 16-01-2012 02:37, Mail Mantis wrote:
> 2012/1/15 Alex Rønne Petersen<xtzgzorex at gmail.com>:
>> Hi,
>>
>> I don't know how many times I've made the mistake of passing a local
>> variable to a function which takes a 'ref' parameter. Suddenly, local
>> variables/fields are just mutating out of nowhere, because it's not at all
>> obvious that a function you're calling is taking a 'ref' parameter. This is
>> particularly true for std.utf.decode().
>>
>> Yes, I realize I could look at the function declaration. Yes, I could read
>> the docs too. But that doesn't prevent me from forgetting that a function
>> takes a 'ref' parameter, and then doing the mistake again. The damage is
>> done, and the time is wasted.
>>
>> I think D should allow 'ref' on call sites to prevent these mistakes. For
>> example:
>>
>> string str = ...;
>> size_t pos;
>> auto chr = std.utf.decode(str, ref pos);
>>
>> Now it's much more obvious that the parameter is passed by reference and is
>> going to be mutated.
>>
>> Ideally, this would not be optional, but rather *required*, but I realize
>> that such a change would break a *lot* of code, so that's probably not a
>> good idea.
>>
>> Thoughts?
>>
>> --
>> - Alex
>
> I don't see how call cite ref will help the issue. If you don't read
> ddoc, how will you know what exactly function does with it's ref
> parameter - statistically? Knowing that function takes parameter by
> ref doesn't help understanding it's logic. Also, knowing that function
> doesn't take any ref parameters doesn't mean you don't need to read
> it's documentation.

You may remember what the function does, but forget that it mutates an 
input parameter.

--
- Alex


More information about the Digitalmars-d mailing list