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: 17

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:4303
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:4310
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:4319
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:Int) Source: stdlib.ngs:4331
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:4342
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:7456
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:7463
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:4378
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)
del(t:TmpFsObj, kcp:KeepCleanupPolicy, e:Box) Source: stdlib.ngs:5890
Undocumented
del(t:TmpFsObj, rcp:RemoveCleanupPolicy, e:Box) Source: stdlib.ngs:5895
Undocumented
del(t:TmpFsObj, koecp:KeepOnErrorCleanupPolicy, e:Box) Source: stdlib.ngs:5905
Undocumented
ExitCode(b:Box) Source: stdlib.ngs:4224
Zero for FullBox and one for EmptyBox