[Issue 22154] Pure functions should be able to use only the address of a global

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 28 17:45:07 UTC 2021


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

--- Comment #2 from Andrei Alexandrescu <andrei at erdani.com> ---
(In reply to Mathias LANG from comment #1)
> If we allow it, what prevents the function from then passing it down to
> another pure function that will read / write to it ?

Good point. There'd need to be appropriate restrictions, e.g. the address is
read but not passed around.

This issue prevents the application of the Null Object Pattern
(https://en.wikipedia.org/wiki/Null_object_pattern) to Appender in Phobos. I
started modifying the code around
https://github.com/dlang/phobos/blob/master/std/array.d#L3264 like this:

    private static __gshared Data nullData = { 0, null, false };
    private Data* _data = &nullData;

It worked really nice because subsequently I could eliminate most tests
comparing _data against null - they just worked with the empty data.

The one place I had to test was at
https://github.com/dlang/phobos/blob/master/std/array.d#L3356, where I replaced
`if (!_data)` with `if (_data != &nullData)`. That made the function impure and
caused a bunch of compile-time errors for no good reason.

--


More information about the Digitalmars-d-bugs mailing list