can we un-deprecate .ptr on arrays in @safe code? cf issue 18529

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 27 20:09:24 UTC 2018


On 2/27/18 3:00 PM, Steven Schveighoffer wrote:
> On 2/27/18 12:32 PM, Atila Neves wrote:
> 
>> There's a common case where it's not equivalent - when the pointer is 
>> null. Imagine I have a C function I want to call:
>>
>> extern(C) void fun(int* things);
>>
>> Imagine also that it's ok to call with null. Well, now I can't use a 
>> slice to call this and have it be 1) @safe and 2) not throw 
>> RangeError. I ran into this the other way.
> 
> fun(x.length ? &x[0] : null);

Hm... borrowing from Timothee's suggestion:

@trusted @nogc pure nothrow
T* pointer(T)(T[] a){
    return a.length > 0 ? a.ptr : null;
}

This would be fine and @safe, but may not be useful for all purposes. 
However, it would fix your issue.

-Steve


More information about the Digitalmars-d mailing list