Re: true false


#1

I was reading up on the Primer, and noticed that “false” is the only accepted value for false, and 0 is not accepted, and true is the only accepted value for true, and 1 is not accepted.

It goes on to say: “If you really need to however, you can explicitly convert from a Boolean type to an Integer type using the conversion operator true>>Integer or calling the conversion method true.Integer – and the reverse 0>>Boolean or 0.Boolean.”

In all honesty, I much prefer that false is either “false” or “0”… as I am not too keen on having to run conversions every time I want to do a conditional, and the same goes with true as 1. It has to do with my own internalized thought process about true(1) and false(0) and not having that is almost definitely going to mess with my natural internalized way of thinking about code as well as not properly writing it when I need to.

Is there not a way to modify this behavior or perhaps some shortcut to having to do true>>integer or 0>>Boolean in conditionals so I can have this functionality?

I would even be willing to addend the top of every .sk file that I touch if necessary with some expression or trick that might make this happen. I just don’t have a strong enough grasp quite yet to pull such a trick out of my hat just yet… so anything would help.


#2

SkookumScript conditionals (if, when and unless) expecting a Boolean type (true or false) and not allowing coercion (automatic conversion) from an Integer (or other types) is as designed. :madsci:

This also the case in several other languages - especially modern ones - such as Java and Swift - see Swift’s Bool type.

Historically, languages such as C didn’t even have a Boolean type and so used a convention of 0 being false and other values being true. This was widely believed to be the cause of many hard to notice errors and Boolean/bool types were introduced, though the old behavior of auto-converting integer values to a Boolean were kept so as to not break existing code bases.

In SkookumScript, if you really want to convert Integer objects to Boolean objects there are two common ways to do so:

// Compare the number to zero
!num : 1
if num ~= 0 [ println("It's non-zero!") ]
// Convert the number to a Boolean
!num : 1
if num>> [ println("It's true!") ]

// This is the same as:
if num>>Boolean [ println("It's true!") ]
// You just need to specify that a conversion is
// wanted and the type does not need to be specified
// (it can be inferred) if the desired type is known.

You can read more about SkookumScript type conversion in the online Primer.