Thread overview
Implicit conversion of struct to bool for if (s) operation ?
Jun 06, 2016
chmike
Jun 06, 2016
John
Jun 06, 2016
chmike
Jun 07, 2016
Basile B.
Jun 06, 2016
Adam D. Ruppe
Jun 07, 2016
Ali Çehreli
Jun 08, 2016
Mike Parker
June 06, 2016
Hello,

I have a structure with two fields ad defined as

struct Info {
    this(int value, Category category)
    {
        category_ = category;
        value_ = category ? value : 0;
    }
    ...
private:
    Category category_ = null;
    int value_ = 0;
}


I would like an implicit conversion of Info to bool that return false if category_ is null so that I can write

Info s = foo();
if (s) {
   ...
}

and where

Info s2;
assert(!s2);


June 06, 2016
On Monday, 6 June 2016 at 15:23:50 UTC, chmike wrote:
> Hello,
>
> I have a structure with two fields ad defined as
>
> struct Info {
>     this(int value, Category category)
>     {
>         category_ = category;
>         value_ = category ? value : 0;
>     }

      // This converts implicitly to bool.
      T opCast(T : bool)() {
          return category_ !is null;
      }

>     ...
> private:
>     Category category_ = null;
>     int value_ = 0;
> }
>
>
> I would like an implicit conversion of Info to bool that return false if category_ is null so that I can write
>
> Info s = foo();
> if (s) {
>    ...
> }
>
> and where
>
> Info s2;
> assert(!s2);

See the inserted code above.
June 06, 2016
On Monday, 6 June 2016 at 15:23:50 UTC, chmike wrote:
> I would like an implicit conversion of Info to bool that return false if category_ is null so that I can write

add:

bool opCast(T : bool)() {
   return whatever;
}

to the struct and it should work.
June 06, 2016
On Monday, 6 June 2016 at 15:28:35 UTC, John wrote:
Thank you John and Adam. That was a quick answer !


June 07, 2016
On Monday, 6 June 2016 at 15:34:18 UTC, chmike wrote:
> On Monday, 6 June 2016 at 15:28:35 UTC, John wrote:
> Thank you John and Adam. That was a quick answer !

Too late but another option would have been to put an alias this on a bool getter:

struct Info
{
    bool getStuff()
    {
        return true;
    }

    alias getStuff this;
}
June 07, 2016
On 06/06/2016 08:28 AM, Adam D. Ruppe wrote:
> On Monday, 6 June 2016 at 15:23:50 UTC, chmike wrote:
>> I would like an implicit conversion of Info to bool that return false
>> if category_ is null so that I can write
>
> add:
>
> bool opCast(T : bool)() {
>    return whatever;
> }
>
> to the struct and it should work.

It's news to me that while opCast for all other types is for explicit casting, opCast for bool works for implicit casting.

Ali
June 07, 2016
On 6/7/16 6:15 PM, Ali Çehreli wrote:
> On 06/06/2016 08:28 AM, Adam D. Ruppe wrote:
>> On Monday, 6 June 2016 at 15:23:50 UTC, chmike wrote:
>>> I would like an implicit conversion of Info to bool that return false
>>> if category_ is null so that I can write
>>
>> add:
>>
>> bool opCast(T : bool)() {
>>    return whatever;
>> }
>>
>> to the struct and it should work.
>
> It's news to me that while opCast for all other types is for explicit
> casting, opCast for bool works for implicit casting.

as ag0... mentioned in another thread, opCast is NOT implicitly being invoked here, but rather explicitly.

That is:

bool x = someStruct; // error
if(someStruct) // explicit opCast!bool

-Steve
June 08, 2016
On Tuesday, 7 June 2016 at 22:28:57 UTC, Steven Schveighoffer wrote:
>>
>> It's news to me that while opCast for all other types is for explicit
>> casting, opCast for bool works for implicit casting.
>
> as ag0... mentioned in another thread, opCast is NOT implicitly being invoked here, but rather explicitly.
>
> That is:
>
> bool x = someStruct; // error
> if(someStruct) // explicit opCast!bool
>

Documented under 'Boolean Operations' at http://dlang.org/spec/operatoroverloading.html#cast