@safe(bool)

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 17 10:21:16 PDT 2017


On 17.08.2017 18:36, HyperParrow wrote:
> On Thursday, 17 August 2017 at 16:32:20 UTC, bitwise wrote:
>> This came to mind while working on a set of containers.
>>
>> [...]
>> One solution could be this:
>>
>> struct Container(T, bool safetyOn = true)
>> {
>>     static if(safe)
>>         RefCounted!(T[]) data;
>>     else
>>         T[] data;
>>
>>     auto opSlice() @safe(safetyOn) {
>>         return Range(data, 0, data.length);
>>     }
>> }
>>
>> A similar solution could be applied to @nogc as well.
> 
> Yeah, i like it more than 
> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1012.md.

That makes little sense to me, as DIP 1012 is strictly more general.

template safety(bool safetyOn){ // (this can even be in core)
     static if(safetyOn) alias safety = FunctionSafety.safe;
     else alias safety = FunctionSafety.system;
     // else alias safety = infer!FunctionSafety; // even better!
}

struct Container(T, bool safetyOn = true){
     static if(safe) RefCounted!(T[]) data;
     else T[] data;

     auto opSlice() @safety!safetyOn {
         return Range(data, 0, data.length);
     }
}


More information about the Digitalmars-d mailing list