Would like to see ref and out required for function calls

Manuel manuel at d.org
Thu Sep 13 12:13:21 PDT 2012


On Thursday, 13 September 2012 at 15:18:06 UTC, Jonathan M Davis 
wrote:
> On Thursday, September 13, 2012 12:34:34 Andrei Alexandrescu 
> wrote:
>> I don't think there would be problems with allowing ref/out 
>> optionally
>> at the call site. The thing is, however, that in this matter 
>> reasonable
>> people may disagree.
>
> I really think that optionally allowing ref and out at the call 
> site is more
> damaging than beneficial. _Requiring_ it could be beneficial, 
> since then you
> know that the arguments are being taken by ref, but if it's 
> optional, it gives
> you a false sense of security and can be misleading. 
> Presumabyl, any time that
> ref or out is used at a call site, presumably the compiler 
> would check that it
> really was passed by ref our out, which would be of some 
> benefit, but it would
> mean _nothing_ if ref or out wasn't used, since it could be an 
> argument being
> passed by ref but which wasn't marked or an argument which 
> wasn't passed by
> ref at all. So, if you have code where ref and out is used at 
> the call site
> semi-religiously, then it'll become very easy to falsely think 
> that the lack
> of them means that ref or out _isn't_ being used when it really 
> is, misleading
> people about what the code is doing.
>
> So, if it wasn't going to break so much code, I'd see some 
> argument for
> changing things so that ref and out were required at the call 
> site, but I
> think that _allowing_ them but not requiring them would 
> actually be
> detrimental.
>
> - Jonathan M Davis

I see your point here but, as David already argued in his post, 
it would only be detrimental if people would not realise that 
these annotations are not obligatory. If that is clearly stated, 
i don't see so much of a problem here.

E.g. if we have a function / method call:

transmogrify(myValueType1, myValueType2, myValueType3);

Right now we don't know whether any of these values is going to 
be modified or not. And with _optional_ annotations allowed we 
couldn't say neither. That situation is not worse than the 
situation we have now. Things would only be bad if people would 
wrongly think that call site annotations are obligatory.


But if we would see the function call:

transmogrify(myValueType1, myValueType2, out myValueType3);

in some unknown piece of code we could at least surely know that 
myValueType3 is going to be modified. We couldn't say anything 
about myValueType1 or myValueType2 with optional annotations. The 
other parameters could or couldn't be modified. But we have at 
least a bit more context / information (e.g. that the function 
has side-effects). Normally functions / methods have better names 
so we would have even more context.


If it would be possible to allow call site annotations for the 
people who want to use them as an additional documentation / 
safety feature and maybe make the compiler emit warnings (through 
an optional compiler switch) if they are omitted or just allow 
them, as long as it's clear that they are optional, i don't see 
any problem with it.




More information about the Digitalmars-d mailing list