Add `&` operator

This commit is contained in:
Emery Hemingway 2023-12-28 18:18:06 +02:00
parent 867d25afee
commit 10fc78172e
1 changed files with 19 additions and 0 deletions

View File

@ -123,6 +123,25 @@ func isEmbedded*(pr: Value): bool {.inline.} =
## Check if ``pr`` is an embedded value.
pr.embedded or pr.kind == pkEmbedded
proc `&`*(x, y: Value): Value =
## Concatenate operator.
if x.kind != y.kind:
raise newException(ValueError, "cannot concatenate heterogenous values")
case x.kind
of pkString:
result = Value(kind: pkString, string: x.string & y.string)
of pkByteString:
result = Value(kind: pkByteString, bytes: x.bytes & y.bytes)
of pkSequence:
result = Value(kind: pkSequence, sequence: x.sequence & y.sequence)
else:
raise newException(ValueError, "cannot concatenate this value type")
proc `&`*(x: Value; y: seq[Value]): Value =
if x.kind != pkSequence:
raise newException(ValueError, "cannot concatenate to non-sequence value")
result = Value(kind: pkSequence, sequence: x.sequence & y)
proc getOrDefault(pr, key: Value): Value =
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
## or returns the `#f` Preserves value.