The motivation for const (the big picture)

Dan murpsoft at hotmail.com
Sun Jan 6 21:25:34 PST 2008


Bill Baxter Wrote:
> 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.
[snip]

Invariant allows for alot of assumptions to be made about something; but it's because we assume merrily that it's okay for the program to crash if something changes out from under us even if the program is paused.

So we can:
- fearlessly run parallel functions over the same data
- cache something and expect it to be correct indefinitely
- trust our libraries, and 3rd parties not to mangle our stuff
- catch accidental writes to something we wanted read-only
- make guarantees to clients and employers (our code *cannot* change your x)
- be aware the opposite is probably true for non-invariants

In all honesty though, we must know it's a bluff.  The code *can* change out from under you.  It just won't by your own hand.

Regards,
Dan



More information about the Digitalmars-d mailing list