www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15615] New: Creating a Variant with a const primitive type

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

          Issue ID: 15615
           Summary: Creating a Variant with a const primitive type doesn't
                    compile
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: initrd.gz gmail.com

Trying to construct a variant with a constant value type (i.e. the value is
copied so the const doesn't matter) fails with a compiler error.

Example:

    import std.variant;

    alias Value = Algebraic!(long, double);

    void main() {
        const long foo = 123L;
        long bar = foo;
        Value baz = Value(foo);
    }

 rdmd ~/test.d
/usr/include/dmd/phobos/std/variant.d(544): Error: static assert "Cannot store a const(long) in a VariantN!(8LU, long, double)" /home/col/test.d(9): instantiated from here: __ctor!(const(long)) Failed: ["dmd", "-v", "-o-", "/home/col/test.d", "-I/home/col"] The same issue occurs if `foo` is immutable, or a double or boolean. However, it does work with structs or slices: import std.variant; struct MyStruct { int a, b; } alias Value = Algebraic!(MyStruct, string); void main() { const MyStruct foo = MyStruct(1, 2); MyStruct bar = foo; Value baz = Value(foo); const string foo2 = "asdf"; string bar2 = foo2; Value baz2 = Value(foo2); } --
Jan 26 2016