What purpose to const besides functional programming?

Sean Kelly sean at invisibleduck.org
Wed Jul 23 09:29:36 PDT 2008


superdan wrote:
> Jason House Wrote:
> 
>> Walter Bright Wrote:
>>
>>> Jason House wrote:
>>>> What *other* purposes does the const system serve besides
>>>> opening the door to functional programming?  I understand that the
>>>> const system must remain simple to be usable.
>>> 1. It makes function APIs self-documenting.
>>> 2. Invariance allows one to pass large data structures around by 
>>> reference yet treat them as if one passed them by value. Strings are the 
>>> canonical example of that.
>>> 3. Const allows one function to service both mutable and invariant 
>>> arguments.
>>> 4. Invariant data can be placed in hardware protected readonly memory, 
>>> such as ROM.
>>> 5. Invariant data does not have to be synchronized to be multithread 
>>> accessible.
>>> 6. Invariant data improves the ability of automated tools to make 
>>> inferences about what is happening. Optimization is an example of such.
>> #5 is not true.  Invariant functions require synchronization because they can manipulate global state.  If #5 is restricted to direct data access without using properties or member functions, then I'd agree with it.  Of course, that qualification eliminates most of the benefit from #5. notice how walter says "data" and you say "function" and you'll pretty much have understood where the problem in your reasoning was.
> 
> of course #5 is true. looks like there was and still is a basic misunderstanding of data vs. function behavior with regard to immutability. 
> 
> don't confuse data with functions. invariant data -> can't modify it even with the sas. that means no synchronization on data necessary. invariant function -> applies to invariant object and may or may not require synchronization. but sure as shit invariant data does not need synchronization.

To be fair, invariant data created at runtime does need synchronization 
before passing it to another thread.  But once the thread has it then no 
more synchronization is necessary to access it.  This is one aspect of 
invariant that I think has been largely overlooked thus far, perhaps 
partially because most people still have single core machines and/or are 
using x86 machines, which have a pretty strict memory model.


Sean



More information about the Digitalmars-d mailing list