On Tuesday, December 18, 2018 5:42:12 AM MST rikki cattermole via Digitalmars-d-learn wrote: > On 19/12/2018 1:34 AM, Per Nordlöw wrote: > > On Tuesday, 18 December 2018 at 10:42:51 UTC, Jonathan M Davis wrote: > >> Unfortunately, D does not currently have a way to do that. Only > >> functions can be marked with @trusted. However, the typical approach > >> to this problem is to use a lambda, which is more or less > >> syntactically the same except that it has some extra parens. e.g. > >> > >> () @trusted { doUnsafe(); }(); > > > > Is there a performance hit when using this? > > Yes except for ldc with -O3. > But it is a function not a delegate so it should be all nicely > prefetched and ready to go. So I wouldn't worry about it too much.
Really? I would have thought that that would be a pretty obvious optimization (especially if inlining is enabled). I suppose that it doesn't entirely surprise me if dmd does a poor job of it, but I would have expected ldc to do better than that. I would think that such an improvement would be pretty low-hanging fruit for adding to dmd's optimizer though. If not, it sounds like further justification for adding a feature for this rather than having to use a lambda. Aside from that though, the lambda trick is simple enough that it wouldn't surprise me if Walter and Andrei's response to such a DIP would be to just use the lambda trick. - Jonathan M Davis