Using Boolean values in Gosu
From Gosu code, two types represent the values true and false:
- The Java primitive type
boolean. Possible values aretrueandfalse. - The Java
Booleanobject, which is an object wrapper around the primitive type. Possible values for variables declared to theBooleandata type aretrue,false, andnull. The fully qualified type name isjava.lang.Boolean. Becausejava.lang.Booleanis always in scope, your code usesBooleanunless 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 |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If you coerce |
|
|
|
|
See note for column value |
|
|
See note |
If coerced to the If coerced to the |
The Be careful to check for |
Notice the following differences between primitive and object types:
nullcoerced to a variable of typeBooleanstores the original valuenull.nullcoerced to a variable of typebooleanstores the valuefalsebecause primitive types cannot benull. 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