Using Boolean values in Gosu

From Gosu code, two types represent the values true and false:

  • The Java primitive type boolean. Possible values are true and false.
  • The Java Boolean object, which is an object wrapper around the primitive type. Possible values for variables declared to the Boolean data type are true, false, and null. The fully qualified type name is java.lang.Boolean. Because java.lang.Boolean is always in scope, your code uses Boolean unless a similarly named type is in scope and requires disambiguation.

For both boolean and Boolean, Gosu coerces values to true or false. The following table describes coercion rules.

Value

Type of value

Coerces to

Note

1

int

true

0

int

false

"true"

String

true

If you coerce String data to either Boolean or boolean, the String data itself is examined. It coerces to true if and only if the text data has the value "true".

"false" or any non-null String value other than "true"

String

false

See note for column value "true".

null

See note

If coerced to the boolean type, the result depends on the type of the declared variable. For some types, including Object and number types such as Integer, null coerces to the value false. For other types, coercion is prohibited at compile time.

If coerced to the Boolean type, value remains null.

The boolean type is a primitive and can never contain null. The Boolean type is an object type, and variables of that type can contain null.

Be careful to check for null values for variables declared as String. A null value might indicate uninitialized data or other unexpected code paths.

Notice the following differences between primitive and object types:

  • null coerced to a variable of type Boolean stores the original value null.
  • null coerced to a variable of type boolean stores the value false because primitive types cannot be null. For some variable types, this coercion is prohibited at compile time.

Be careful to check for null values for variables declared as String to avoid ambiguity in your code. A null value might indicate uninitialized data or other unexpected code paths.

Example

var hasMoreMembers : Boolean = null
var isDone = false