ddonche/goblin-lang
0.46.24
1
0
docs reference
[[bool]]

Bool


bool confirms that a value is a boolean and returns it unchanged.


Overview

Use bool when a value must already be either true or false:

is_ready | true.bool

:say(is_ready)  /// true
is_ready | true.bool

:say(is_ready)  /// true

bool is a strict cast. It does not guess whether another kind of value should mean true or false.


Method Style

The usual casting style places .bool after the value:

enabled | true.bool
disabled | false.bool
enabled | true.bool
disabled | false.bool

The method form does not need parentheses.

The equivalent free-call form is:

enabled | bool(true)
enabled | bool(true)

Both forms behave the same way.


Strict Boolean Values

Only actual boolean values are accepted:

true.bool   /// true
false.bool  /// false
true.bool   /// true
false.bool  /// false

Numbers are not converted to booleans:

1.bool
/// error: R0215 type-lock-cast
1.bool
/// error: R0215 type-lock-cast

Strings are not converted either:

"true".bool
/// error: R0215 type-lock-cast
"true".bool
/// error: R0215 type-lock-cast

This prevents values such as 1, 0, "yes", and empty strings from silently receiving a meaning the program did not state.


Parsing Boolean Text

Use Parse Bool when a string should be interpreted as a boolean:

answer | "true".parse_bool

:say(answer)  /// true
answer | "true".parse_bool

:say(answer)  /// true

bool checks or casts a boolean value. parse_bool reads a boolean from text.


Arrays and Maps

When given an array, bool checks every value and returns a new array:

flags | [true, false, true].bool
flags | [true, false, true].bool

When given a map, bool checks every value and keeps the keys unchanged:

settings | {
    music: true,
    subtitles: false
}.bool
settings | {
    music: true,
    subtitles: false
}.bool

Every value in the collection must already be a boolean:

[true, 1, false].bool
/// error: R0215 type-lock-cast
[true, 1, false].bool
/// error: R0215 type-lock-cast

The original collection is not changed.


Signature

value.bool
bool(value)
value.bool
bool(value)

Argument Type Description
value boolean, array, or map The boolean value or collection of boolean values to check.

bool returns the boolean unchanged, or returns a collection containing the checked boolean values.


Errors

bool requires exactly one value:

bool()
/// error: R0301 wrong-arity
bool()
/// error: R0301 wrong-arity

The value must already be a boolean:

bool("false")
/// error: R0215 type-lock-cast
bool("false")
/// error: R0215 type-lock-cast

Every value inside an array or map must also be a boolean.