D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5256 - null Rebindable testing not supported
Summary: null Rebindable testing not supported
Status: RESOLVED DUPLICATE of issue 4773
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2010-11-21 18:55 UTC by bearophile_hugs
Modified: 2011-07-30 11:03 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-11-21 18:55:50 UTC
This D2 program shows that you can't test if a class reference wrapped with Rebindable is null:


import std.typecons;
const class Foo {}
void main() {
    auto a = Rebindable!Foo(new Foo);
    a = new Foo;
    assert(a !is null); // err
}


DMD 2.050 generates:
test.d(6): Error: incompatible types for ((a) !is (null)): 'Rebindable!(const(Foo))' and 'void*'


Is this the currently correct way to do it? (It works):

assert(a.get() !is null); // OK

I have seen the get() method is not documented on the site, so is that a temporary limitation caused by the unfinished "alis this" implementation?

(Issue tagged with "Component: DMD" because I think it's a limit of "alias this").
Comment 1 bearophile_hugs 2011-07-30 11:03:08 UTC
assert(a !is null); is wrong code.

Bug 4773 is now fixed. Now you are allowed to write:


import std.stdio, std.typecons;
const class Foo {
    invariant() { writeln("*"); }
}
void main() {
    auto a = Rebindable!Foo(new Foo);
    a = new Foo;
    //assert(a); // calls Foo.invariant()
    //assert(cast(bool)a); // doesn't call Foo.invariant();
    auto f = new Foo();
    assert(f); // calls Foo.invariant()
    assert(cast(bool)f); // doesn't call Foo.invariant();
}


So I consider this bug too fixed.

*** This issue has been marked as a duplicate of issue 4773 ***