Stepping back and looking at constness from another angle.

Bill Baxter dnewsgroup at billbaxter.com
Tue Jun 5 04:15:51 PDT 2007


David B. Held wrote:

Most of your post was great, but this bit seems to be a bit off to me:

> 3) Function parameters should have the same syntax as local declarations
> 
> Why?  Well, why not?  Why should users have to think hard about whether 
> a particular syntax is only legal for parameters or not?  While certain 
> qualifiers like 'in' and 'out' must necessarily be exceptions, they idea 
> is that they provide sufficient convenience to justify their existence. 
>  Beyond that, you should be able to look at a function parameter list 
> and say: "Each of those parameter declarations could be pulled out into 
> a local declaration".

Current D doesn't have this property.
void foo(out int bar) {
    out int baz; // oops 'out' syntax only works on parameters!
}

nor does C++:
void foo(int x[5]) { // sort of works but likely not what user expected
     int y[5];
}


Maybe you were arguing in the abstract, though, that it would be nice if 
it were true?   I don't really see why it should be the case though.  I 
don't have to think hard now about whether or not I can use 'out' on a 
local variable.  As long as the rules make sense users won't have to 
think that hard.  It's pretty easy to remember something like 
"parameters aren't usually modified so they're const default, locals are 
so they're mutable default".  Probably even easier to remember and more 
intuitive than the differences between structs and classes.

--bb



More information about the Digitalmars-d mailing list