[Dlang-internal] DIP1000 discussion and testing

Walter Bright via Dlang-internal dlang-internal at puremagic.com
Sun Dec 18 08:49:02 PST 2016


On 12/18/2016 8:23 AM, Mathias Lang wrote:
> I didn't meant as a standalone example, but in the context of the previous
> snippet. Full code:
>
> ```
> int bar () @safe
> {
>     int i = 42;
>     Foo f;
>     f.v = &i;
>     return f.foo(); // Doesn't compile
> }
>
> struct Foo
> {
>     int* v;
>     int foo () @safe { return *v; }
> }
> ```

That doesn't compile because the Foo could be written as:

static int* s;
struct Foo
{
     int* v;
     int foo () @safe { s = v; return *v; }
}

The compiler can't know that from the interface to Foo.foo, and so must assume 
the worst. The safety features are about guarantees, not having faith that the 
programmer didn't do something like that.

The purpose in adding the 'scope' annotation to Foo.foo is to tell the compiler 
that there's no hanky-panky going on in the implementation. When the compiler 
does compile the implementation, it can then check it against the 'scope' semantics.


More information about the Dlang-internal mailing list