Array as an argument, ambiguous behaviour.

Dicebot public at dicebot.lv
Thu Jan 30 04:47:55 PST 2014


On Wednesday, 29 January 2014 at 14:34:54 UTC, Cooler wrote:
> Here is ambiguity.
> void fun3(int[] x){ x ~= 5; ... }
> auto a = new int[10];
> fun3(a); // Here content of "a" may be changed or may be not 
> changed. Depends on the buffer size that system will allocate 
> for "a" array.

You use very subjective meaning for "ambiguity", one that is 
never normally used in programming language context. Normally 
term "ambiguity" is applied only when _compiler_ can't reliably 
decide meaning of the code snippet and needs to resort to special 
casing.

You are correct in notion that you can't make any reasonable 
judgement yourself about content of a after `fun3` call but same 
applies to `fun2` - it can be or not be modified. It is expected 
- to express all semantics of function body in function signature 
one would have needed to make it of comparable length and 
complexity.

In practice you need always to assume the worst - everything is 
modified and nothing can be trusted unless explicitly qualified 
other way around.


More information about the Digitalmars-d-learn mailing list