Thread overview
[Issue 8486] Possibly wrong interaction of Variant and const arrays
Oct 18, 2019
berni44
Oct 18, 2019
David Nadlinger
Oct 18, 2019
berni44
Dec 17, 2022
Iain Buclaw
October 18, 2019
https://issues.dlang.org/show_bug.cgi?id=8486

berni44 <bugzilla@d-ecke.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@d-ecke.de

--- Comment #6 from berni44 <bugzilla@d-ecke.de> ---
This seems not to be a Phobos bug:

import std.stdio;

void main()
{
    const int[] arr;
    writeln(typeof(arr).stringof);
    Test(arr);
}

struct Test
{
    this(T)(T value)
    {
        writeln(T.stringof);
    }
}

writes:

const(int[])
const(int)[]

For me the question remains: Is this intentional (for reasons I don't understand yet) or a language bug?

--
October 18, 2019
https://issues.dlang.org/show_bug.cgi?id=8486

--- Comment #7 from David Nadlinger <code@klickverbot.at> ---
(In reply to berni44 from comment #6)
> For me the question remains: Is this intentional (for reasons I don't understand yet) or a language bug?

Removal of "head const", i.e. the outer layer, is intentional. This avoid duplicate template instantiations for code that has identical semantics anyway (like for a const int parameter, where it is copied anyway even if non-const, so from the caller side, there isn't a difference), and allows range functions to work in a natural way with constness (where, again, the slice is copied anyway, so const or not isn't visible to the caller).

--
October 18, 2019
https://issues.dlang.org/show_bug.cgi?id=8486

--- Comment #8 from berni44 <bugzilla@d-ecke.de> ---
(In reply to David Nadlinger from comment #7)
> Removal of "head const", i.e. the outer layer, is intentional.
OK. In that case this is a Phobos bug. The solution would be to make peek remove "head const" on the given type und check if that's the type of the Variant. I've got too few experience on const, so I cannot fix this. I tried to add a small template which returns what it get's (just out of curiosity, if that could work). With this, peek could compare the type of the returned value to the value of the Variant. But this did not work out well. Partly, because the template needs to be given a value and not a type, partly because of inout gets in the way.

I don't know if it's of much use, because this can be easily derived from the post above, but I created this unittest which currently does not pass but a corrected version of peek should pass:

unittest
{
    const int[] arr;
    Variant a = Variant(arr);
    assert(a.peek!(typeof(arr)) !is null);
}

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=8486

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P3

--