The motivation for const (the big picture)

Bill Baxter dnewsgroup at billbaxter.com
Sun Jan 6 16:23:43 PST 2008


Jason House wrote:
> I've also heard (but never seen
> justification/explanation) that invariant helps with functional
> programming.

I think it only helps in the sense that it makes automatic 
parallelization of functional-style programs possible.  If I foreach 
over some data structure and all the inputs are invariant, then the 
compiler is free to do that in any order or in parallel because it knows 
that executing a function on one element cannot possibly change the 
value of another element by any means.  With a C++ readonly style const 
you only know that you're not changing it, but there are a dozen ways 
that it could be changed out from under you in sneaky and unexpected, 
but perfectly legal, ways.

That said, I'm not sure what that's going to look like in D.  It seems 
like making use of invariant for functional programming in D will 
require a totally different programming style where you make everything 
invariant all the time.  It doesn't seem like you'll be able to just 
"toss in" a little FP to an existing project without a lot of redesign.

--bb



More information about the Digitalmars-d mailing list