Add `&` operator
This commit is contained in:
parent
c89e6b5d3d
commit
f3eb6d3fb8
|
@ -123,6 +123,25 @@ func isEmbedded*(pr: Value): bool {.inline.} =
|
||||||
## Check if ``pr`` is an embedded value.
|
## Check if ``pr`` is an embedded value.
|
||||||
pr.embedded or pr.kind == pkEmbedded
|
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 =
|
proc getOrDefault(pr, key: Value): Value =
|
||||||
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
|
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
|
||||||
## or returns the `#f` Preserves value.
|
## or returns the `#f` Preserves value.
|
||||||
|
|
Loading…
Reference in New Issue