Const: what do you want to achieve (proposition...)?
David B. Held
dheld at codelogicconsulting.com
Sun Nov 18 19:43:22 PST 2007
Gilles G. wrote:
> You misunderstood the proposition: it implies that all parameters given to a function would be const *except* if they are returned by the function.
> So, in the example you give, if you wanted to be sure that val is not modified, you would just call:
> myFunctionReturningVoid(val);
>
> A function which wants to change its arguments would *have to* pass them as return values.
>
> For example this function:
> Foo myFunction(Foo foo){
> foo.value = 0;
> return foo;
> }
> could be called like this:
> myFunction(myFoo); // don't modify myFoo since parameters are const by default
> myFoo2 = myFunction(myFoo); // still don't modify myFoo
> myFoo = myFunction(myFoo); // this is the *only* way to modify myFoo: it is explicit
> It would be up to the compiler to decide if the parameters must be passed by value, reference, (or whatever) to acheive constness.
> [...]
From a pure functional point of view, that's all well and good. But D
allows in-place mutation, and that means functions have to be allowed to
have out parameters that look like out parameters.
Dave
More information about the Digitalmars-d
mailing list