@trusting generic functions

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 28 05:43:27 PDT 2016


On Saturday, 28 May 2016 at 12:33:28 UTC, Era Scarecrow wrote:
> On Saturday, 28 May 2016 at 12:25:14 UTC, Lodovico Giaretta 
> wrote:
>> On Saturday, 28 May 2016 at 11:57:09 UTC, Era Scarecrow wrote:
>>>     auto doSomethingDumb(T)(ref T t) if(isSafe!(T))
>>
>> The problem is that T is a type, and I should check for safety 
>> of every method of T that I'm using in my function. This does 
>> not scale well, and if I change the body of the function to 
>> use a new method, I may forget to add it to the isSafe checks.
>
>  Hmmm that doesn't sound right. Safe code can't call unsafe 
> code (or it shouldn't), it continues @safe/@trusted all the way 
> down. So if you are passed a safe/trusted function, then it 
> shouldn't need other checks.
>
>  Or am i reading the question wrong?

Sorry for not being clear. Look at this example:

struct S1
{
     int doSomething() @safe
     {
         // do something safely
         return 1;
     }
}

struct S2
{
     int doSomething() @system
     {
         // do something usafe
         return 2;
     }
}

auto doSomethingDumb(T)(ref T t)
{
     T* pt = &t;
     return pt.doSomething();
}

auto s1 = S1();
auto s2 = S2();
auto x = doSomethingDumb(s1); // this call should be possible in 
@safe code
auto y = doSomethingDumb(s2); // this should only be possible in 
@system code


More information about the Digitalmars-d-learn mailing list