Interested in D, spec confuses me.

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 2 17:42:34 PST 2016


On Tue, 02 Feb 2016 15:41:07 -0800, H. S. Teoh via Digitalmars-d wrote:

> Furthermore, since const provides actual guarantees that the called
> function isn't going to touch the data, this opens up optimization
> opportunities for the compiler.

const opens up optimizations at the call site, so it's useful. immutable 
is useful on top of const because it allows optimizations within the 
function.

Want to memoize a function? If it takes const(char[]), you have to copy 
your input and later check the entire array to see if the parameters 
match a previous call. If it takes an immutable(char[]), you can compare 
pointers.

Do you need to acquire a lock to read this data? If it's immutable, no, 
you don't.


More information about the Digitalmars-d mailing list