Logical expressions

Gosu logical expressions use standard logical operators to evaluate the expression in terms of the boolean values of true and false.

Gosu supports the following logical expressions:

  • Logical AND
  • Logical OR
  • Logical NOT

Gosu evaluates logical expressions from left to right and uses the following rules to test the expressions for possible short-circuit evaluation:

  • true OR any-expression always evaluates to true – Gosu only runs and evaluates any-expression if the expression before the OR is false. If Gosu determines that the expression before the OR evaluates to true, the second expression is not evaluated.
  • false AND any-expression always evaluates to false – Gosu only runs and evaluates any-expression if the expression before the AND is true. If Gosu determines that the expression before the AND evaluates to false, the second expression is not evaluated.

Logical AND

Gosu uses either and or && to indicate a logical AND expression. The operands must be of the Boolean data type or any type convertible to Boolean. The result is always Boolean.

Syntax

a and b
a && b

Examples

Expression

Result

(4 > 3) and (3 > 2)

(true/true)   = true

(4 > 3) &&  (2 > 3)

(true/false)  = false

(3 > 4) and (3 > 2)

(false/true)  = false

(3 > 4) &&  (2 > 3)

(false/false) = false

Logical OR

Gosu uses either or or || to indicate a logical OR expression. The operands must be of the Boolean data type or any type convertible to Boolean. The result is always Boolean.

Syntax

a or b
a || b

Examples

Expression

Result

(4 > 3) or (3 > 2)

(true/true)   = true

(4 > 3) || (2 > 3)

(true/false)  = true

(3 > 4) or (3 > 2)

(false/true)  = true

(3 > 4) || (2 > 3)

(false/false) = false

Logical NOT

To indicate a logical negation (a logical NOT expression), use either the keyword not or the exclamation point character (!). The operand must be of the Boolean data type or any type convertible to Boolean. The result is always Boolean.

Syntax

not a
!a

Examples

Expression

Result

!true

false

not false

true

!null

true

not 1000

false

The following examples illustrate how to use the NOT operator.

Bad example
The following example demonstrates how not to use the logical NOT operator.
if (not liabilityCov.Limit == line.BOPLiabilityCov.Limit) { 
  return true
}

This example causes an error at run time because Gosu associates the NOT operator with the variable to its right before evaluating the expression. This association causes the expression to become:

(false == line.BOPLiabilityCov.Limit) 

This comparison causes a class-cast exception, as follows:

'boolean (false)' is not compatible with Limit
Better example
The following example demonstrates how to use the NOT operator correctly.
if (not (liabilityCov.Limit == line.BOPLiabilityCov.Limit)) { 
  return true
}

In this example, the extra parentheses force the desired comparison, and associate the NOT operator with the correct expression.

Preferred example
Use the != operator rather than NOT for writing code of this type.
if (liabilityCov.Limit != line.BOPLiabilityCov.Limit) { 
  return true
}

The expression had no need to use the NOT operator. The final code expression is somewhat simpler and does exactly what is required.

typeis expressions

Gosu uses the operator typeis to test the type of an object against a named type. The result is true if the object has that type or a subtype.

See also