diff --git a/src/preserves.nim b/src/preserves.nim index 6d18dfb..a499610 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -175,11 +175,20 @@ proc hash*(pr: Preserve): Hash = !$h proc `[]`*(pr: Preserve; i: int): Preserve = - ## Select an indexed value from `pr`. + ## Select an indexed value from ``pr``. ## Only valid for records and sequences. case pr.kind of pkRecord: pr.record[i] of pkSequence: pr.sequence[i] + else: + raise newException(ValueError, "`Preserves value is not indexable") + +proc `[]`*(pr, key: Preserve): Preserve = + ## Select a dictionary value from ``pr``. + if pr.kind == pkDictionary: + for (k, v) in pr.dict: + if key == k: return v + raise newException(KeyError, "Key not in Preserves dictionary") else: raise newException(ValueError, "`[]` is not valid for " & $pr.kind)