Const: what do you want to achieve (proposition...)?

David B. Held dheld at codelogicconsulting.com
Tue Nov 13 22:55:26 PST 2007


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?

Essentially, you're overloading the meaning of operator= in a very 
non-intuitive way.  It's an interesting idea, but not very D-like.

Dave



More information about the Digitalmars-d mailing list