Would like to see ref and out required for function calls

Nick Treleaven ntrel-public at yahoo.co.uk
Wed Sep 12 05:55:03 PDT 2012


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, I completely missed this :-/

(But disallowing UFCS here still might be a good solution).


More information about the Digitalmars-d mailing list