Box type

A box which might (FullBox) or might not (EmptyBox) contain a value. Box is somewhat similar to Arr of zero (EmptyBox) or exactly one (FullBox) element. This analogy helps understanding each() and filter() operation.

Direct parent types

Eachable1
Eachable which each() calls the callback with one argument
Direct subtypes: 16

Direct children types

EmptyBox
Represents absence of a value
FullBox
Represents presence of a value
Direct subtypes: 8

Constructors

Box(x:Any) Source: stdlib.ngs:2915
Convert anything to Box (always FullBox)

Parameters

xvalue to enclose in a box

Returns

FullBox with given value

Example

Box(7).map(X*2).get()  # 14
Box() Source: stdlib.ngs:2922
Convert Box with no arg to EmptyBox

Returns

EmptyBox

Example

Box() == EmptyBox()
Box().get(10)  # 10
Box().map(X*2).get()  # InvalidArgument exception (in "get()")
Box(n:Null) Source: stdlib.ngs:2931
Convert null to Box (always EmptyBox)

Returns

EmptyBox

Example

Box(null) == EmptyBox()
Box(null).map(X*2).get()  # InvalidArgument exception (in "get()")
Box(a:Arr, idx:Any) Source: stdlib.ngs:2944
Convert array value indexed by the given index

Parameters

idxkey to look in hash

Returns

Box. FullBox if the array has the element indexed by idx, EmptyBox otherwise.

Example

my_array = [10, 20]
my_array.Box(1).map(X*2).each(echo)
# output: 40
my_array.Box(5).map(X*2).each(echo)
# no output
Box(h:Hash, k:Any) Source: stdlib.ngs:2962
Convert hash value indexed by the given key to a Box

Parameters

kkey to look in hash

Returns

Box. FullBox if the hash has the element referenced by k, EmptyBox otherwise.

Example

my_hash = {"a": 10, "b": 300}
my_hash.Box("a").map(X*2).each(echo)
# output: 20
my_hash.Box("nope").map(X*2).each(echo)
# no output
Box(s:Success) Source: stdlib.ngs:5573
Convert Success to FullBox

Returns

FullBox

Example

Box("abcd" ~ /^(..)/).map({"First two letters: ${A[1]}"}).get("(too short)")  # First two letters: ab
Box(f:Failure) Source: stdlib.ngs:5578
Convert failure to EmptyBox

Returns

EmptyBox

Example

Box("a" ~ /^(..)/).map({"First two letters: ${A[1]}"}).get("(too short)")  # (too short)

Methods

==(b1:Box, b2:Box) Source: stdlib.ngs:2998
Compare boxes. Empty boxes and boxes with same content are considered to be equal.

Returns

Bool

Example

EmptyBox() == EmptyBox()
EmptyBox() != FullBox(1)
FullBox(1) == FullBox(1)
FullBox(1) != FullBox(2)
ExitCode(b:Box) Source: stdlib.ngs:2846
Zero for FullBox and one for EmptyBox