Casting an expression to bool means testing for 0 or !=0 for arithmetic types

Pluto pluto at planets.not
Sat Jul 31 16:22:46 PDT 2010


== Quote from Dmitry Olshansky (dmitry.olsh at gmail.com)'s article
> On 31.07.2010 23:44, Pluto wrote:
> > This part has always bothered me. Could somebody please explain to me the
> > rationale behind limiting functions to one usable error code?
> >
> > if(function())
> > ~~
> >
> Inherently if is for testing _condition_ which is true/false. Going
> futher you'd just reinvent switch statement.
> which if perfectly OK for it, here it goes:
>      switch(function()){
>          case ERR_CODE1:
>              // ---
>          break;
>          case ERR_CODE2:
>              // ---
>          break;
>              // ---
>          default:
>              // ---
>          break;
>      }

But these aren't compatible for the same function.
Defining false as <1 would fix this.

> Honestly, I'd suggest using exceptions instead of error codes.
> Usage of error codes scales poorly and itself is very error-prone, also
> killing the return value of functions just for error code leads to very
> awkward design.
I was asking purely out of design interest.
~~
:Pluto



More information about the Digitalmars-d-learn mailing list