On 12/18/18 6:29 AM, Simen Kjærås wrote:
On Tuesday, 18 December 2018 at 10:14:50 UTC, Per Nordlöw wrote:
What's the preferred way of creating a temporary @trusted scope
without writing a separate function?
Jonathan's idea might also be encapsulated in a function, just like
assumeUnique in Phobos:
import std.stdio;
template unsafe(alias fn) {
@trusted auto unsafe(T...)(T args) {
return fn(args);
}
}
@system void fun(int n) {
writeln("foo!");
}
@safe unittest {
unsafe!({
fun(2);
});
unsafe!fun(2);
}
Wow, I really like this. The only real problem is that one generally
searches for @trusted when looking for unsafe code, but unsafe is pretty
obvious too. Also, args should be auto ref.
Only thing better I can think of (for the second example) is to define a
prototype with the correct information, only with @trusted (and specify
the mangle). But any decent compiler should get rid of any overhead there.
-Steve