www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13168] New: cast(ref T) should work, along with support from

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 puremagic.com
          Reporter: andrei 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

--
Jul 20 2014