[Issue 13168] New: cast(ref T) should work, along with support from opCast

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jul 20 11:31:19 PDT 2014


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

          Issue ID: 13168
           Summary: cast(ref T) should work, along with support from
                    opCast
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: andrei at erdani.com

Consider:

void main()
{
    int a;
    cast(ref uint) a = 42;
}

This doesn't compile. What people usually do is:

void main()
{
    int a;
    *cast(uint*) &a = 42;
}

but this is more dangerous and less tractable especially in safe code.
Furthermore, opCast support should work seamlessly:

struct A
{
    int a;
    ref int opCast(int)() { return a; }
}
A x;
cast(int) x = 42; // assigns to x.a

This is confusing and should only work if the cast specifies ref:

cast(ref int) x = 42; // fine, assign to x.a

--


More information about the Digitalmars-d-bugs mailing list