I'm implementing a database cursor class. The keys are basically strings. A cursor can be restricted to a sub-range of the keys, like you might want to iterate only from "X" to "Y".
I thought of using subscripts with ".." for this, so curs["X".."Y"] would return a copy of curs that's restricted to the given range.
However, this needs to support open-ended ranges, where only one side is given. But Nim's Slice types don't support this — you can't do .."Y" or "X"...
I'm wondering whether any existing Nim module has this kind of API, and if there's any existing convention on how to represent this?
(In the meantime, I'm not using slices, just plain old firstKey and lastKey properties you can set.)
Hm, no postfix operators...
I'm leaning toward using nil for the "alpha/omega" sentinels. (That's more or less how they're already represented internally.)
So, for (key, value) in collection[nil .. "x"]:
or for (key, value) in collection["a" .. nil]:
(I realize nil isn't a valid string value. The type isn't actually string, it's something convertible to/from string.)
Well it appears that you can define a [.. ]: https://play.nim-lang.org/#ix=2Erd
Unfortunately, it seems you are not allowed to define at the same time a [ ..].
instead of nil you could use something like this:
type
PositiveInf = object
NegativeInf = object
proc `..`(a: typedesc[NegativeInf], b: int) = discard
proc `..`(a: int, b: typedesc[PositiveInf]) = discard
which then can be used like this:
0..PositiveInf
NegativeInf..0