Interested in D, spec confuses me.

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 3 06:29:30 PST 2016


On 2/2/16 8:42 PM, Chris Wright wrote:
> 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.

This isn't exactly right. If I call a function with "hello", I'd want it 
to memoize if the function is called with "hello" that resides elsewhere.

What *is* true is that you can safely save the array (pointer + len) of 
"hello" and be sure it won't change when you check against it later. 
With const(char[]), you'd need to allocate a new block to make sure it 
doesn't change.

-Steve


More information about the Digitalmars-d mailing list