The Status of Const

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 16 16:44:11 PDT 2010


On Mon, 16 Aug 2010 19:06:56 -0400, Simen kjaeraas  
<simen.kjaras at gmail.com> wrote:

> Jonathan M Davis <jmdavisprog at gmail.com> wrote:
>
>> On Monday, August 16, 2010 15:31:23 Steven Schveighoffer wrote:
>>> We can move onto the next test:
>>>
>>> Given:
>>>
>>> class C {}
>>>
>>> class D : C {}
>>>
>>> convert Array!D into Array!(const C)
>>>
>>> OK, maybe this is a more advanced test :)
>>>
>>> I'd be happy with just proper factoring of const to be honest.
>>
>> Um, I don't think that I've ever seen that work in _any_ language, and  
>> I'm not
>> sure that it would be a good thing if it did. Array!D is a completely  
>> different
>> type from Array!C or Array!(const C). I could see an argument that  
>> Array!D
>> should work as Array!(const D), but that seems off to me. Converting  
>> Array!D to
>> const (Array!D) should work though.
>
> Uhm, it works in D:
>
> class C {}
> class D : C {}
>
> void main( ) {
>      D[] a;
>      C[] b = a;
> }

Yes, I was about to say that except that is a bug kind of.  The type  
system should only allow casting D[] to const(C)[].

See http://d.puremagic.com/issues/show_bug.cgi?id=2095

But aside from that, the test is invalid, I don't think D* can implicitly  
cast to C* (I'm not sure).  This might be a built-in array-only thing.   
The compiler is going to be hard-pressed to get that one right anyways, as  
it involves changing types as Jonathan said.

This kind of thing is tricky to get right.

BTW, I thought about it more, and I don't think  
Rebindable!(const(Array!V)) is going to work.  The public interface that  
rebindable exposes is the const version, and you want to expose the const  
version only for the references, and expose the mutable version for the  
values.  Rebindable only allows reassigning the whole struct at once.

I feel more and more strongly that this has to be a compiler solution.  A  
library solution may eventually be possible, but it's going to be very  
nasty/tricky to write.  I wouldn't be surprised if using a library tail  
const type adds 10K to the executable size for each type it's used on...

-Steve


More information about the Digitalmars-d mailing list