On 30.08.20 17:24, outlandkarasu wrote:
--------
enum Tag { tag = "tag" }

struct A { Tag tag; }

A createA() @safe
{
     scope a = A(Tag.tag);

     // Error: scope variable a may not be returned
     return a;

     // NG
     // return A(a);
     // return A(a.tag);
}
--------
[...]
I understand those errors are DIP1000 language design.
However I suppose that DIP1000 check can permit immutable pointer in some cases.

If I understand correctly, your point is that an enum pointer is guaranteed to refer to static data, so it could be exempt from `scope` checks.

At a glance, that makes sense to me. But I guess one question is whether it's possible to create an enum value that points to the stack. A cast does the trick:

    immutable char[1] c = 'e';
    E e = cast(E) c[];

DMD accepts it as @safe, implying that the cast is valid and that `e` is a safe value. If that is correct, then enum pointers are actually not guaranteed to refer to static data. They can just as well point to the stack. Consequently, an enum pointer must be treated like a plain pointer. I.e., `scope` must treat a `Tag` just like a plain `string`.

Is there a better workaround, practices or patterns?

In your example, you can just remove the `scope` annotation. Why mark a local that you want to return with `scope`? Doesn't make sense

But I guess your actual use case isn't as simple. Maybe you can show a less reduced version of the code where simply removing `scope` is not an option?

Reply via email to