Advertisement

Expression must be of boolean type?

Started by February 25, 2014 11:55 AM
3 comments, last by SiCrane 10 years, 9 months ago

?

I'm just wondering why the following code fails with "Illegal operation on this datatype" and "Expression must be of boolean type" errors. I understand what the errors mean. I'm more so wondering why the VM isn't smart enough to be able to evaluate zero as false and non-zero as true? Unless I'm missing something?


int test = 0;
if(!test)
   test = 1;


… why the VM isn't smart enough to be able to evaluate zero as false and non-zero as true ...

I'm not familiar with AngelCode, but using 0 as boolean false and any other integer value as boolean true is a convention introduced by programming languages that does not support a boolean type natively. If a boolean type is supported, then any automatic conversion to boolean is a language feature. Not doing so allows for greater type safety, doing so allows for greater convenience. I won't say that a particular language is not smart because it lacks such automatic conversion.

Advertisement

It is like this by design.

I chose not to allow implicit conversion of any type to or from bool in AngelScript because it is a very common cause for bugs in C++, especially with beginner programmers.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

OK, thanks guys. Just wanted to make sure I understood what was going on.

Sorry for using "not smart." I'm not an expert on the inner workings of languages and compilers. Thanks for the info haegarr.

Fun examples of languages that don't follow the zero is false convention are ruby, where every integer including zero evaluates to true (only False and nil evaluate as false) and most shell scripting languages where zero is true and non-zero is false.

This topic is closed to new replies.

Advertisement