The motivation for const (the big picture)

Bill Baxter dnewsgroup at billbaxter.com
Sun Jan 6 21:35:09 PST 2008


Dan wrote:
> 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.

So you're saying the likely usage patterns will involve giving the 
compiler "hints" in the form of casts to invariant on data that actually 
isn't?

--bb



More information about the Digitalmars-d mailing list