Stepping back and looking at constness from another angle.

Charlie charlie.fats at gmail.com
Tue Jun 5 06:02:43 PDT 2007


Johan Granberg Wrote:

> David B. Held wrote:
> 
> > Let me point out a few guiding principles that are worth considering:
<snip>
> 
> You write a loot about Haskell and how awkward it is to use pure functional
> constructs in a systems language like D. But I have not heard anyone say
> that they want to make D pure in any way. Even functional languages like
> Standard ML have reference parameters (mutable) and arrays (also mutable)
> and what was suggested for D was even less extream, if you are OK whit
> writing const all over the place how can writing mutable all over the place
> be any worse. The idea is that if you forget to declare something const the
> compiler cant catch you but if you forget to declare mutable the compiler
> will complain if you try to change it. I don't know about your programs but
> when I code in imperative languages like C D or Java most variables are
> never changed they are defined locally as the result of some computation or
> function call and read one ore more time during the rest of the function,
> granted globals and class members are a little different but I don't think
> they are more common than function parameters and if you are OK whit
> writing const next to each function parameter writing mutable next to some
> member variables should not be a huge burden.

I think you've hit it on the head here, the problem is the lack of const on a parameter tells you nothing, where as if const was the default ( can we agree that most parameters are usually passed const ? ) ,then thet lack of a keyword means its safe, and the presence of a keyword means the function plans to modify it.  Its 100% clear what the function plans to do, you can't get that with const .

Charlie



More information about the Digitalmars-d mailing list