Regarding opCast(bool) class method

Regan Heath regan at netmail.co.nz
Fri Nov 9 02:50:10 PST 2012


On Thu, 08 Nov 2012 23:04:17 -0000, bearophile <bearophileHUGS at lycos.com>  
wrote:
> If this situation is a problem, then here are two of the possible  
> solutions:
> - Do not allow opCast(bool) in classes. How much often do you need  
> cast(bool) on class instances?

I don't like it, it restricts people from defining a class which  
represents a boolean value/state (and casting it to bool).

> - Keep cast(bool) in classes, and remove the asymmetry between structs  
> and classes, if possible. So "if(fclass)" on a class instance calls  
> opCast(bool). Then to test the value of the reference you use "if(fclass  
> is null)".

This is a breaking change.

A slightly less breaking change would be to make "if (fclass)" into both a  
reference !is null check and a call to any opCast(bool) if it exists.   
This would then only break existing code using "if (fclass)" on references  
to classes which define opCast(bool) (which return false).

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list