Implicit cast to bool

Started by
2 comments, last by FDsagizi 11 years, 6 months ago
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
}
Advertisement
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

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.
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.smile.png

This topic is closed to new replies.

Advertisement