Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Expression must be of boolean type?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 wilberolive   Members   -  Reputation: 301

Like
0Likes
Like

Posted 25 February 2014 - 05:55 AM



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;


Sponsor:

#2 haegarr   Crossbones+   -  Reputation: 4746

Like
0Likes
Like

Posted 25 February 2014 - 07:27 AM


… 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.



#3 Andreas Jonsson   Moderators   -  Reputation: 3498

Like
0Likes
Like

Posted 25 February 2014 - 03:17 PM

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

#4 wilberolive   Members   -  Reputation: 301

Like
0Likes
Like

Posted 25 February 2014 - 11:38 PM

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.



#5 SiCrane   Moderators   -  Reputation: 9728

Like
0Likes
Like

Posted 26 February 2014 - 12:04 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS