Would like to see ref and out required for function calls

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


On 11/09/2012 18:57, David Piepgrass 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
>>>
>>> For UFCS, ref should be implied.
> +1
>
>> Why? UFCS means uniform function call syntax.
> It is already understood that the thing left of '.' may be passed by
> reference:
>
> struct Foo { int x = 0; void f() { x++; } }
> void obvious()
> {
>     Foo foo; foo.f(); // x is passed to f() by reference
> }
>
> Perhaps your argument makes sense for classes, but not for structs. In
> any case the syntax (ref foo).f() would require extra work for Walter so
> I would not propose it. What I might propose instead is that, if the
> user requests (via command-line argument such as '-callSiteRef') that a
> warning be issued for arguments passed without 'ref' at the call site,
> then a situation like this should prompt a warning.
>
> 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.


More information about the Digitalmars-d mailing list