@trusted considered harmful

Jonathan M Davis jmdavisProg at gmx.com
Sat Jul 28 13:12:40 PDT 2012


On Saturday, July 28, 2012 22:08:42 David Nadlinger wrote:
> On Saturday, 28 July 2012 at 02:33:54 UTC, Jonathan M Davis wrote:
> > @property auto save()
> > {
> > 
> >     import std.conv;
> >     alias typeof((*_range).save) S;
> >     static assert(isForwardRange!S, S.stringof ~ " is not a
> > 
> > forward range.");
> > 
> >     @trusted
> >     {
> >     
> >         auto mem = new void[S.sizeof];`
> >         emplace!S(mem, cast(S)(*_range).save);`
> >         return RefRange!S(cast(S*)mem.ptr);
> >     
> >     }
> > 
> > }
> > 
> > That is _way_ cleaner.
> 
> But unfortunately wrong – you call S.save in the @trusted
> block… ;)

Yeah. I screwed that up. I was obviously in too much of a hurry when I wrote 
it. And actually, in this particular case, since the part that can't be 
@trusted is in the middle of an expression doing @system stuff, simply using an 
@trusted block wouldn't do the trick. The basic principle is still good 
though. Being able to mark specific statements or sections of code @trusted 
would greatly simplify templated functions which are making function calls 
which _can't_ necessarily be @trusted.

- Jonathan M Davis


More information about the Digitalmars-d mailing list