Do pure functions solve the "return const" problems?

Christopher Wright dhasenan at gmail.com
Tue Apr 29 06:55:15 PDT 2008


Bruno Medeiros wrote:
> Christopher Wright wrote:
>>
>> Right. But since you can trivially rewrite every function with out 
>> parameters as one that has multiple return values, it's reasonable to 
>> expect a compiler to fully support pure functions with out parameters.
>>
> 
>   void func(out int a, out int b) {
>     a = 2;
>     b = 10;
>   }
> 
> How do you rewrite this function with multiple return values? (You 
> can't, because of the case where there is aliasing of the arguments, 
> which wouldn't have the same result if the function was rewritten with 
> multiple return values)
> 

That is interesting, and the first reasonable argument I've heard.

You can't just record, for a given function, what the precedence of the 
out parameters is:

void func(out int a, out int b, bool c) {
	if (c) {
		a = 5;
		b = 9;
	} else {
		b = 12;
		a = 3;
	}
}

You could return a struct containing ordering information, but that's 
too ugly to consider.

Though this brings up a point that out parameters are more expressive 
than multiple return values. While this is not likely to make a 
difference in most reasonable usage, it's still the case, and might 
therefore be an argument in favor of keeping out parameters for pure 
functions.

The other argument would be, out parameters already exist and apparently 
are used for large structs and multiple return values already, so it'd 
be trivial to implement -- in point of fact, it'd take more effort to 
prevent out parameters for pure functions.



More information about the Digitalmars-d mailing list