Would like to see ref and out required for function calls

Timon Gehr timon.gehr at gmx.ch
Mon Sep 10 13:36:38 PDT 2012


On 09/09/2012 02:54 PM, Nick Treleaven wrote:
> On 08/09/2012 19:17, Timon Gehr wrote:
>> On 09/08/2012 06:05 PM, Nick Treleaven wrote:
>>> On 08/09/2012 14:05, Chris Nicholson-Sauls wrote:
>>>> Given:
>>>>
>>>> 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.
>>
>> Why? UFCS means uniform function call syntax.
>
> I meant if callsite ref was required for parameters, it should not be
> required for array in 'array.func()'. It would be bizarre to require
> '(ref array).func()'.
>

Not more bizarre as in other positions.

>...
>>
>> This is not necessarily a valid conclusion. Popularity does not imply
>> importance.
>
> I only said it was *arguably* important, given that the parent post said
> the idea failed to gain traction. Go and Rust are relatively new and
> decided for the explicit callsite approach (AFAIU).
>

Yes, Go uses explicit pointer types.
Regarding Rust, you are wrong.
I have built the latest Rust compiler.

import io;

fn modify(&a:int){
     a = 2;
}
fn swap<T>(&a:T,&b:T){
    let tmp<-a;
    a<-b;
    b<-tmp;
}

fn main(){
    let mut a= 1;
    io::println(fmt!("%d",a)); // "1"
    modify(a);
    io::println(fmt!("%d",a)); // "2"
    let mut b=2, c=3;
    io::println(fmt!("%d %d",b,c)); // "2 3"
    swap(b,c);
    io::println(fmt!("%d %d",b,c)); // "3 2"
    let mut f = fn@(a:int)->int{ a + 1 };
    let mut g = fn@(a:int)->int{ a - 1 };
    io::println(fmt!("%d %d",f(2),g(2))); // "3 1"
    swap(f,g);
    io::println(fmt!("%d %d",f(2),g(2))); // "1 3"
}


> ...
>>
>> What is this claim based on? The use case above is probably one of the
>> more common ones.
>
> How often do you use swap?
>

Whenever I want to swap the values of two variables. All other
functions in my current project except one that use ref either pass by
const/immutable ref, or all arguments are ref, or they are function
pointer literals that are part of a polymorphic value type where it is
obvious that the first parameter is passed by ref. The remaining
function is private and declared right between the methods that use it.




More information about the Digitalmars-d mailing list