From 10fc78172e2dff76fd431c32f520142fd7b11bf1 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 28 Dec 2023 18:18:06 +0200 Subject: [PATCH] Add `&` operator --- src/preserves.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/preserves.nim b/src/preserves.nim index 8d9940e..2c27d25 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -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.