const challenge

Janice Caron caron800 at googlemail.com
Fri Feb 1 22:35:56 PST 2008


On 2/1/08, Sergey Gromov <snake.scaly at gmail.com> wrote:
> The possibility to cast should still be there for complex cases where you
> can't/don't want to put data allocation into return statement but can
> guarantee its uniqueness.

I disagree. For type safety, it should be required wherever a
non-unique type is assigned to a unique type. By necessity, this must
include function returns. Thus

    unique(char)[] f()
    {
        char[] s = whatever();
        return cast(unique)s;
    }

In this case, the cast to unique is unavoidable, since, if you omit
it, you would be attempting to implicitly cast s (a char[]) to the
return type, upon return. This cast is required for type safety, and
justified because the code does guarantee uniqueness (because s goes
out of scope on return, so at that point there will be no references
to the data apart from the return value).

It is almost impossible to /prove/ uniqueness, so it is reasonable to
rely on programmer assertion (casts). What the compiler /can/ do, and
do well, is ensure type safety.



More information about the Digitalmars-d mailing list