const

Jason House jason.james.house at gmail.com
Fri Mar 28 14:52:23 PDT 2008


Walter Bright Wrote:

> Jason House wrote:
> > In my dream world implementation of all things const, D would do the following things:
> >   readonly (ro, or some variant) would mean a read only view
> >   invariant would mean the data never changes, period
> >   manifest declares a manifest constant
> >   const would have two meanings:
> >     1. Variable declarations - a true constant
> >     2. Function parameters - a promise not to modify the data
> 
> That's 5 different kinds of constants (as opposed to the current 3). I 
> can hear the protests already <g>.

I'd hope that I only added one new kind of const.  If I added more than that, then I screwed up :)  Let me explain what I'm thinking.

If we ignore the whole manifest constant thing, there's two kinds of constness right now.  This leads to people defining const and invariant versions of functions, and defining functions with const and invariant versions of function arguments.  Not really knowing how stuff will be used, or what kind of data will be present, they almost have to start mixing and matching, or just declare a few const variations and ignore the rest.  Just imagine a function with 4 parameters that won't get modified.  Should the coder provide 2^4=16 permutations of the same function?

What's novel about what I was posting is that I want a way to have polysemous constness...  A promise by the code writer to not modify the data, but saying *nothing* about what happens to the data outside of it's control.  It's different than the current const type where you won't assume the data is invariant.  Instead, it's saying that both const and invariant data is ok as an input and leaves it to be the compiler's job to do the right thing.

I may be adding a new type of const, but it's a lazier form that I believe many D users would prefer to use.  I'd expect this new type of polysemous const to be used very widely and push explicit declaration of an exact const format away from the every day user.

In effect, I'm increasing the const types the *compiler* has to worry about but reducing what an average D user has to worry about to one const type!



More information about the Digitalmars-d mailing list