Returning dynamic array from the function
Jonathan M Davis
jmdavisProg at gmx.com
Wed Oct 17 13:07:13 PDT 2012
On Wednesday, October 17, 2012 21:46:50 bearophile wrote:
> sclytrack:
> > It doesn't give an error when marking the function with safe.
> >
> > @safe
> > int[] create()
> > {
> > }
>
> I think marking it @safe is not relevant. In theory a good type
> system should give an error message on similar code. I don't know
> if D is supposed to spot similar error situations.
@safe is irrelevant, because the code is just plain broken in the first place.
It really should be an error (
http://d.puremagic.com/issues/show_bug.cgi?id=7087 ), just like it's an error
to return a pointer to a local variable. But unfortunately, since all it takes
to trick the compiler is passing the slice (or pointer in the case of the
pointer to a local variable) to a function which then returns that
slice/pointer, there's no way for the compiler to always catch it for you.
The only way that @safe could really be applicable would be if it became
@system to take the address of a local variable or to slice a static array.
And perhaps it should be, but that and catching the most obvious cases are all
that the compiler could do to catch this for you.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list