https://issues.dlang.org/show_bug.cgi?id=15594

--- Comment #1 from Chris Wright <dhase...@gmail.com> ---
The .object() and .array() methods are not and cannot be @safe or @trusted
because they return by reference. Observe:

---
void main()
{
  JSONValue v;
  v.type = JSON_TYPE.OBJECT;
  auto o = &v.object();
  v.str = "overwrite";
  (*o)["world"] = "i hope this works";
}
---

This will, naturally, end in a segmentation fault.

A different strategy is to create a @safe / @trusted DiscriminatedUnion
template in std.typecons and use that. We'd have to expose @system methods to
retrieve items by reference to make .object and .array continue to work, which
is kind of ugly.

Anyway, making str() @trusted and adding a couple @trusted methods, objectNoRef
and arrayNoRef, mostly works.

The main problem is iteration. The options are:
 * opApply is @system. That sucks.
 * opApply is @safe. That means you can only use it in @safe code.
 * Provide @safe and @system overloads. The compiler isn't smart enough to use
the right one with foreach syntax.
 * Switch to ranges. I can only transparently do this with arrays *or* objects,
not both, so this is also a breaking change.
 * opApply is @system. Use .array and .object in @safe code. Oh wait, they're
@system because they return by reference. Expose .arrayNoRef and .objectNoRef
instead. That's kind of crufty but works.
 * Expose .arrayNoRef and .objectNoRef, deprecate .array and .object, and
eventually replace them with the NoRef variants.

--

Reply via email to