Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Implicit cast to bool


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
3 replies to this topic

#1 Jake Albano   Members   -  Reputation: 596

Like
0Likes
Like

Posted 17 October 2012 - 08:47 PM

What is the reasoning behind disallowing implicit value casts to bool? Is there a better way to do the same thing?

I'm looking to be able to do something like this:

Keystate@ state = keyboard.getKeystate("space");

// elsewhere...
if (state)
{
   // key is down
}


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3220

Like
1Likes
Like

Posted 18 October 2012 - 01:13 PM

The reasoning is that the implicit conversion to bool in C/C++ is a very common bug with less experienced programmers.

Why not add a function, e.g. isDown() to the Keystate object? It would make the code more readable to those unfamiliar with it.

Keystate@ state = keyboard.getKeystate("space");
// elsewhere...
if ( state.isDown() )
{
   // key is down
}

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

#3 Jake Albano   Members   -  Reputation: 596

Like
0Likes
Like

Posted 18 October 2012 - 02:57 PM

That's probably the best solution in this case, and it is definitely more readable. I was hoping to use implicit bool casting for other purposes, but I think the limitation might actually help me write idiomatic code.

#4 FDsagizi   Members   -  Reputation: 514

Like
0Likes
Like

Posted 19 October 2012 - 08:22 AM

I support Andreas Jonsson, he right!
I used to not like this code:

SomeClassPtr @p;

if( p !is null )
{
}

but then I realized that so immediately clear!
For scripts this is best way.Posted Image




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