3.2. kink/BOOL

Companion mod for bool vals.

3.2.1. type bool

Bool vals are true or false, which are the results of kink/CORE.true and kink/CORE.false respectively.

if_else and if_true methods are used as two-way and one-way branch. For example:

:turn_switch <- {(:On?)
  On?.if_true{
    stdout.print_line('Turn on!')
  }
  On?.if_else({ 'on' } { 'off' })
}

stdout.print_line(turn_switch(true))
# Output:
#   Turon on!
#   on

stdout.print_line(turn_switch(false))
# Output:
#   off

For multi-way branch, use kink/CORE.branch fun.

op_lognot, op_logand and op_logor methods are used as logical operators. For example:

# whether S has a whitespace character on the head or the tail
:strippable? <- {(:S)
  ! S.empty? && (S.get(0) == 0x20 || S.get(S.size - 1) == 0x20)
}

stdout.print_line(strippable?('foo').repr)  # => false
stdout.print_line(strippable?(' foo').repr) # => true
stdout.print_line(strippable?('foo ').repr) # => true
stdout.print_line(strippable?('').repr)     # => false

$strippable? fun in the program above is translated as the following in the process of compilation.

:strippable? <- {(:S)
  S.empty?.op_lognot.op_logand{ (S.get(0) == 0x20).op_logor{ S.get(S.size - 1) == 0x20 } }
}

Bool.if_else($on_true $on_false)

Bool.if_else does a two-way branch.

If Bool is true, Bool.if_else tail-calls $on_true with no args.

If Bool is false, Bool.if_else tail-calls $on_false with no args.

Bool.if_true($on_true)

Bool.if_true does a one-way branch.

If Bool is true, Bool.if_true tail-calls $on_true with no args.

If Bool is false, Bool.if_true returns null.

Bool.op_eq(Arg_bl)

Bool.op_eq returns whether two bool vals are equal.

Arg_bl must be a bool.

If both Bool and Arg_bl are true, or both Bool and Arg_bl are false, Bool.op_eq returns true.

If Bool is true and Arg_bl is false, or Bool is false and Arg_bl is true, Bool.op_eq returns false.

Note that an expression “X == Y” is translated to “(X).op_eq(Y)”. See Semantics section of the manual.

Bool.op_ne(Arg_bl)

Bool.op_ne returns whether two bool vals are not equal.

Arg_bl must be a bool.

If Bool is true and Arg_bl is false, or Bool is false and Arg_bl is true, Bool.op_ne returns true.

If both Bool and Arg_bl are true, or both Bool and Arg_bl are false, Bool.op_ne returns false.

Note that an expression “X != Y” is translated to “(X).op_ne(Y)”. See Semantics section of the manual.

Bool.op_lognot

Bool.op_lognot does logical NOT operation.

If Bool is true, Bool.op_lognot returns false.

If Bool is false, Bool.op_lognot returns true.

Note that an expression “! X” is translated to “(X).op_lognot”. See Semantics section of the manual.

Bool.op_logand($thunk)

Bool.op_logand does logical AND operation.

If Bool is true, Bool.op_logand tail-calls $thunk with no args.

If Bool is false, Bool.op_logand returns false.

Note that an expression “X && Y” is translated to “(X).op_logand{ (Y) }”. See Semantics section of the manual.

Bool.op_logor($thunk)

Bool.op_logor does logical OR operation.

If Bool is true, Bool.op_logor returns true.

If Bool is false, Bool.op_logor tail-calls $thunk.

Note that an expression “X || Y” is translated to “(X).op_logor{ (Y) }”. See Semantics section of the manual.

Bool.repr

Bool.repr returns "true" for true, "false" for false.

3.2.2. BOOL.bool?(Val)

BOOL.bool? returns whether the Val is a bool val.

Example:

:BOOL.require_from('kink/')
stdout.print_line(BOOL.bool?(true).repr)   # => true
stdout.print_line(BOOL.bool?(false).repr)  # => true
stdout.print_line(BOOL.bool?(42).repr)     # => false