Bool to Float

Monkey Forums/Monkey Bug Reports/Bool to Float

ElectricBoogaloo(Posted 2013) [#1]
This isn't a biggy, and I don't expect it to get "fixed" because it's not really a bug, more a quirk.
.. but I spotted it, so I might as well mention it.

a=(b=1)*0.5
doesn't work...

a=float(b=1)*0.5
doesn't work...

a=float(int(b=1))*0.5
does work!


Xaron(Posted 2013) [#2]
Halleluja... lol. I'm just curious how you stumbled across this one?!


ElectricBoogaloo(Posted 2013) [#3]
SetAlpha(0.5+((Status>0)*0.5))


Gerry Quinn(Posted 2013) [#4]
This is working as intended, I think. There is an implicit conversion from bool to int, and from int to float. But implicit conversions do not chain.


marksibly(Posted 2013) [#5]
Yeah, it's a bit of a 'quirk' - bool can only be implicitly cast to int.

Bool casting is a bit dodgy, esp. with strings - eg: for this to work:

Local b:Bool=...
If b=Bool(String(b))
'will always work, right?
Endif

bool->string would have to produce an empty string. If bool->string produced something like "0","1","false","true" whatever, string->bool would always return true since string->bool is the same as string.Length<>0 (which I really like and find useful).

I thought bool->string producing empty strings would be confusing, eg: if you tried to Print( b ) and it printed nothing, so I disallowed bool->string casting entirely.

Implicit bool->float could be made to work, but I decided to keep it simple for now. So yes, it's 'quirky', but IMO less quirky than the alternatives might have been!