const challenge

Janice Caron caron800 at googlemail.com
Fri Feb 1 11:33:59 PST 2008


On 2/1/08, Sergey Gromov <snake.scaly at gmail.com> wrote:
> > You need a way of expressing that functions can accept unique objects,
> > and return them. e.g.
> >
> >     unique(char)[] fillWithSpaces(unique(char)[]s)
> >     {
> >         foreach(ref c;s) c = ' ';
> >         return s;
> >     }
>
> I'm afraid that's not possible if you want strict type safety

What I'm telling the compiler here is that the function's formal
parameter, s, is unique at the callee site. Within the function, it
stops being unique. Thus, within the function body, s is just a
char[]. At the callee site, however, the compiler gets to complain if
a non-unique type is passed to the function.

(My example passed it the return value from new, so it was OK).


> So the only way to return a unique result from a function is to dup
> or new or inline-construct directly within its return statement.  Other
> return values are illegal.

Good point. It should have been

    return cast(unique)s;



More information about the Digitalmars-d mailing list