www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Const: what do you want to achieve (proposition...)?

reply Gilles G. <schaouette free.fr> writes:
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.

Simple, isn't it?



David B. Held Wrote:

 Gilles G. wrote:
 [...]
 and this function may be used like this:
 Bar myBar;
 Foo myFoo;
 Foo myFoo2;
 
 (myBar,myFoo2) = myFunction(myFoo); // don't modify myFoo
 (myBar,myFoo) = myFunction(myFoo); // modify myFoo
 
 Here, the *compiler knows* if it must ensure the constness of myFoo or
 not. And the user of the function knows *explicitly* if myFoo gets
 modified or not.
 [...]

It's not at all obvious to me by looking at either expression that *anything* ought to be const. If I saw a language that allowed that expression (like Perl, or many other languages with first-class tuples/lists), I would not immediately think "Oh, myFoo doesn't get modified in the first case". Quite the contrary, looking at that I would assume that anything could be modified (but mostly because Perl is the first thing that comes to mind and Perl doesn't have a real const yet). Another problem is that your syntax just looks like a way to return a tuple from a function, and doesn't at all imply that it's being used to declare constness. Also, a function that takes 5 const arguments will have to repeat them, making for a very long invokation. Also, a function returning void would cause this to be a very awkward syntax: (val) = myFunctionReturningVoid(val); // Uh...what?

Nov 13 2007
parent "David B. Held" <dheld codelogicconsulting.com> writes:
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
Nov 18 2007