const debacle

Janice Caron caron800 at googlemail.com
Mon Mar 24 05:12:12 PDT 2008


On 24/03/2008, Daniel919 <Daniel919 at web.de> wrote:
> > How do you declare a function that takes an array, is not allowed to change
>  > the array, but returns a slice into the argument array, and the return type
>  > matches the argument type.
>
>         return cast(T) s[start .. end];

That's fine, but you've got an explicit cast in there. Walter said it
"cannot be implemented without casting away const at some point", so
with that cast, you've just kinda demonstrated that he's right.

The explicit cast basically says "I, the programmer, know that this is
safe, so compiler, please ignore your usual rules". And that's fine,
because in this case, it /is/ safe.

The problem is to do it without the explicit cast. The problem is how
to let the compiler prove that it's safe. And that's why, if you don't
want an explicit cast in there, I believe that returning indeces is
the best way to go.



More information about the Digitalmars-d mailing list