By the following code i get a normal Access Violation.
My question is: why? Even if "f0" is null, the object must be
converted to Ref and there i check if the given object is null.

Here is the Code:
[code]
// Ref.d

struct Ref(T : Object) {
private:
        T _val;

public:
        @disable
        this();

        @disable
        this(typeof(null));

        @disable
        typeof(this) opAssign(typeof(null));

        this(T val) {
                assert(val !is null);

                this._val = val;
        }

        @property
        T get() {
                return this._val;
        }
}

mixin template TRef(T) {
        @property
        Ref!(T) getRef() {
                assert(this !is null);

                return Ref!(T)(this);
        }

        alias getRef this;
}

// main.d
class Foo {
private:
        string _val;

public:
        this() { }

        void Set(string val) {
                this._val = val;
        }

        void speak() const {
                writeln(this._val);
        }

        mixin TRef!(typeof(this));
}

void with_ref(Ref!(Foo) rf) {
        rf.get.speak();
}

void without_ref(Foo f) {
        f.speak();
}

void main() {
        Foo f0; // f0 is null

        with_ref(f0); // converting with alias this. But what if f0 is
null?

        Foo f1 = new Foo();
        f1.Set("Foo!");

        with_ref(f1);
        without_ref(f1);

        readln();
}
[/code]

Reply via email to