天天看點

d拒絕@信任問題

​​原文​​

void foo() @trusted
{
    static struct T
    {
        Exception ex;
        ubyte[] buf;
    }

    scope buffer = new ubyte[100];
    T t;

    t.ex = new Exception("hello");
    t.buf = buffer;
    throw t.ex;//不能抛t,
//但這是`@trusted`,`異常`不打算為`域`.
}
//因為,有`@trusted`,應該工作.
//在`@safe`中,因為由于`t.buf = buffer;`,推導`t`為域.是以`抛`會失敗.

void main() @safe
{
    foo();
}
//用-dip1000編譯      

但,傳回值可以是無​

​域​

​​的,是以添加以下​

​通路器​

​可以工作:

static struct T
{
   Exception ex;
   ubyte[] buf;
   Exception getEx() { return ex; }
//好了,現在不局限`域`了.
}

...

throw t.getEx;      
void foo() @safe
{
    ...其餘與最上面相同,略...
    auto helloEx = new Exception("hello");
    t.ex = helloEx;//
    //最上面是直接t.ex = new Exception("hello");
    t.buf = buffer;
    throw helloEx;
}