Hmm - about manifest/enum

Christopher Wright dhasenan at gmail.com
Tue Jan 1 05:29:05 PST 2008


Janice Caron wrote:
> On 12/31/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>>> ...and that would work now.
>> Fair enough, but the solution still fails because I have to use double the
>> space and extra processing, and creating such an array is too complicated.
> 
> Too complicated?
> 
>     const(C)*[] addPointers(const(C)[] a)
>     {
>         const(C)*[] r;
>         foreach(i,e:a) { r ~= a.ptr + i; }
>         return r;
>     }
> 
>     const(C)[] removePointers(const(C)*[] a)
>     {
>         const(C)[] r;
>         foreach(p:a) { r ~= *p; }
>         return r;
>     }
> 
> It's a few extra lines of code, that's all. I'm not saying this is a
> generic solution which will solve every problem you can think of, but
> the point is, there are always workarounds.
> 
> On the other hand, adding a tailconst function to D would wreck the
> type system.
> 
> I wouldn't want a const(int)[] array to be mutable, and likewise I
> wouldn't want a const(C)[] array to be mutable. Once upon a time I
> argued for the syntax "const(C)&[]", but Walter patiently explained
> why that would cause insurmountable problems elsewhere. So it's one of
> those "lesser of two evils" things. Make the programmer think a bit
> and do things differently, versus wreck the type system. I think
> Walter made the right choice.

Insurmountable problems with templates? Not really; you'd just have to 
make the unary post-& more general, referring to the reference for 
classes and an automatically dereferenced pointer for value types. Which 
is a decently large change, but it wouldn't break any existing code.

Then you're basically left with your pointer workaround, but you don't 
have to think about that fact unless you're creating the array.

This wouldn't break the type system, as far as I can tell. It's 
essentially converting reference types to value types when you declare a 
special kind of pointer to them. Or leaving reference types alone and 
converting value types to reference types. But it won't be implemented, 
so there's no point in me discussing it further.



More information about the Digitalmars-d mailing list