Constant relationships between non-constant objects

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 00:11:24 PDT 2014


Jonathan M Davis:

> Head-constness is something that's requested a _lot_ less 
> frequently
> though (in fact, I think that this is the first time that I've 
> seen
> it brought up), so if head-constness were the only issue, I 
> don't
> think that many of us would be all that annoyed at the fact that
> we couldn't do it in the language itself.

Often I have functions that take an array as argument, like this 
foo:

void foo(int[] arr) {}
void main() {}


In many cases I'd like to modify the array contents, but I'd like 
a way to tell the D type system that inside foo I don't want to 
change the length of the array arr, nor that I want to rebind the 
pointer. This is head-const and it's not useful to avoid bugs 
outside foo (because length and ptr of arr are values, so foo 
can't change their value at the calling point), it's useful to me 
to avoid modifying the array length by mistake inside foo:

void foo(int(const[]) arr) {}
void main() {}


So I'd like to use for array Head-constness. An advantage of this 
kind of constness is that it doesn't change the type system a 
lot, because the constness of the length+ptr of arr is not 
visible outside foo.

Bye,
bearophile


More information about the Digitalmars-d mailing list