Shouldn't invalid references like this fail at compile time?

Mike Franklin slavo5150 at yahoo.com
Tue Jan 23 04:11:08 UTC 2018


On Tuesday, 23 January 2018 at 02:25:57 UTC, Mike Franklin wrote:

> Should `destroy` be `@system` so it can't be called in `@safe` 
> code, or should the compiler be smart enough to figure out the 
> flow control and throw an error?

Interestingly, `destroy` is an unsafe operation for classes.

import std.stdio;

class A
{
     void hello() @safe { writeln("hello"); }
}

void main() @safe
{
     A a = new A();
     a.hello();
     destroy(a);  // onlineapp.d(12): Error: @safe function 'D 
main' cannot call
                  // @system function 'object.destroy!(A).destroy'
     a.hello();
}

https://run.dlang.io/is/AwKBc3


But it's not an unsafe operation for structs

import std.stdio;

struct A
{
     int i;
     void print() @safe { writeln(i); }
}

void main() @safe
{
     A* a = new A();
     a.print();  // OK
     a.destroy();
     a.print();  // Error!
}

https://run.dlang.io/is/Fm7qBR

Not sure if that's a bug or not.

Mike


More information about the Digitalmars-d mailing list