Implicit conversion of struct to bool for if (s) operation ?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 7 15:28:57 PDT 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


More information about the Digitalmars-d-learn mailing list