D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4773 - Rebindable should be castable to bool
Summary: Rebindable should be castable to bool
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
: 5256 (view as issue list)
Depends on: 5020
Blocks:
  Show dependency treegraph
 
Reported: 2010-08-31 08:03 UTC by Simen Kjaeraas
Modified: 2011-07-30 11:03 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2010-08-31 08:03:15 UTC

    
Comment 1 Simen Kjaeraas 2010-08-31 08:09:08 UTC
Yeah, that's what you get when you press the wrong buttons. :p

Anyways, Rebindable should be castable to bool, to allow for the idiom Rebindable!T foo; if (foo) {}.

Addition to typecons.d, line 747:

            T opCast(T : bool)()
            {
                return original !is null;
            }
Comment 2 Shin Fujishiro 2010-10-08 13:18:00 UTC
Actually, it's explicitly castable to bool:

Rebindable!(const Object) r;
if (cast(bool) r) assert(0);
r = new Object;
if (!cast(bool) r) assert(0);

But dmd doesn't forward implicit boolean conversions in if (or other) conditions to Rebindable's alias-this object.  Reported: bug 5020.
Comment 3 Kenji Hara 2011-07-26 21:07:23 UTC
This is dmd issue.
Fixing bug5020 is incomplete, so property function + alias this is not implicitly convertible to bool.

----
struct Rebindable
{
    Object obj;
    @property const(Object) get(){ return obj; }
    alias get this;
}

Rebindable r;
if (r) assert(0);
r.obj = new Object;
if (!r) assert(0);
----
Comment 4 Kenji Hara 2011-07-29 19:17:26 UTC
Posted pull request:

https://github.com/D-Programming-Language/dmd/pull/281
Comment 6 bearophile_hugs 2011-07-30 11:03:08 UTC
*** Issue 5256 has been marked as a duplicate of this issue. ***