What exactly does "@safe" mean?

Piotr Szturmaj bncrbme at jadamspam.pl
Sat Jun 1 16:12:53 PDT 2013


W dniu 01.06.2013 23:55, Jonathan M Davis pisze:
> The guarantees of @safe hold only so long as there are no holes in it, but any
> and all holes we find get fixed. Making ref be truly @safe has been a large part
> of the recent ref discussions, as you can currently get away with doing
> something like
>
> ref int id(ref int i) { return i; }
>
> ref int foo()
> {
>      int j;
>      return id(j);
> }

I know that introducing another type qualifier may complicate things
but this would be a compile time solution.

I mean _scope_ type qualifier, so your example could be rewritten as:

ref int id(ref int i) { return i; }

ref int foo()
{
     int j;
     return id(j); // error: could not pass scope(int) as ref int parameter
}

Taking an address of local variable would always yield a scope
qualified type, scope(int) in this example.

Obviously, scope qualified type could be passed to functions taking
scope parameters:

void bar(scope ref int i) { i = 10; }

void foo()
{
     int j = 0;
     bar(j); // ok
     assert(i == 10);
}

I think this could fill the @safety holes.


More information about the Digitalmars-d mailing list