Would like to see ref and out required for function calls

Nick Treleaven ntrel-public at yahoo.co.uk
Fri Sep 14 07:56:04 PDT 2012


On 12/09/2012 13:55, Nick Treleaven wrote:
> On 12/09/2012 13:39, Nick Treleaven wrote:
>>> class Bar { int b; }
>>> void changeBar(ref Bar b) { b = new Bar(); }
>>> void warning()
>>> {
>>>      Bar bar = new Bar();
>>>      bar.b = 10;
>>>      bar.changeBar(); // Warning: 'bar' is implicitly passed by
>>> reference. To eliminate this warning, use 'changeBar(ref bar)' instead
>>> or do not compile with '-callSiteRef'
>>> }
>>>
>>> Again, this problem only applies to classes, since it is understood that
>>> structs are normally passed by reference.
>>
>> I had only thought about UFCS and ref parameters for value types. You
>> are right that requiring callsite ref for class ref parameters would be
>> a useful idea, as modifying the ref itself is unusual behavior. And
>> because of that, disallowing UFCS for functions that have a class ref
>> parameter as the first parameter might be an elegant solution.
>
> Also the same applies to ref parameters with any reference types:
>
> On 08/09/2012 14:05, Chris Nicholson-Sauls wrote:
>  > void func (ref int[], int)
>  >
>  > If ref/out were required at the call site, this destroys UFCS.
>  >
>  > int[] array;
>  > array.func(0); // error, ref not specified by caller
>
> So the above poster was right

Ignore that last reply, slices are value types. The syntax array.func() 
looks like it might modify array's contents and/or length, so UFCS is 
OK. It is only ref class and ref pointer arguments that UFCS should be 
disallowed on to enforce call-site ref.


More information about the Digitalmars-d mailing list