Feature to get or add value to an associative array.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Apr 20 09:24:26 UTC 2018


On Friday, April 20, 2018 10:56:37 ag0aep6g via Digitalmars-d wrote:
> On 04/20/2018 10:24 AM, Nick Treleaven wrote:
> > On Wednesday, 18 April 2018 at 16:47:50 UTC, ag0aep6g wrote:
> >> You can get a pointer from the ref return:
> >>
> >>     Value* p = &aa.slot("key", { inserted = true; return Value.init;
> >> });
> >
> > This is not @safe, even with -dip1000:
> >
> > Error: cannot take address of ref return of f() in @safe function main
>
> Hm. Do you know why that's not allowed? I can't see how it would be less
> safe than returning a pointer, and I can't find a mention of it in DIP
> 1000 [1]. Maybe it's just a case of incomplete/buggy implementation?
>
>
> [1] https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md

The compiler assumes that a pointer is valid when determining whether code
is @safe. It then disallows operations that it can't guarantee are @safe
even if the pointer is valid, and it disallows taking the address in cases
where it can't guarantee that that's @safe. Basically, it verifies the
@safety of pointers when they're created and then assumes that they're @safe
after that. So, passing around a pointer is perfectly @safe, whereas taking
the address of a ref return value to get a pointer is not.

- Jonathan M Davis



More information about the Digitalmars-d mailing list