Sorry for the useless thread title, I couldn't think of a better one.
I'm currently learning Nim and decided that an SDK for Amazon Web Services (AWS) would be a good practice exercise. I've stumbled upon a couple of compile errors while working on it. I've managed to find workarounds, but I don't understand why the original code is invalid and would like help to get my head around it.
I've posted the code in progress here: https://github.com/aidansteele/learning-nim-aws. The specific problematic lines are:
var headerPairs = asKeyVal(toSeq(pairs(varreq.headers)))
#headerPairs = filterIt(headerPairs, it.key != "Host")
headerPairs = filter(headerPairs, proc(it: auto): bool = it.key != "Host")
The commented-out line is the problematic one. Below it is the workaround. filterIt works in other circumstances, but not here. The error is
Error: undeclared identifier: 'it'
The second confusion:
let unsortedPairs = asKeyVal(toSeq(pairs(request.headers)))
let sortedHeaderPairs = sortedByIt(unsortedPairs, it.key)
# let sortedHeaderPairs = sortedByIt(asKeyVal(toSeq(pairs(request.headers))), it.key)
Here is a similar issue. When the code is deeply nested, it fails - but when spread out over two lines, it works. The error is:
Error: internal error: environment misses: result
As a bonus third question, I'm wondering if there's a better solution than the asKeyVal proc. Here it is:
proc asKeyVal*[T](x: T): auto = cast[seq[tuple[key: string, value: string]]](x)
I have this because I want to support StringTableRefs, Table[string, string], OrderedTable[string, string] and so on. pairs(x: StringTableRef) returns a tuple with named fields, Table and friends doesn't. Is there a better way around this than casting?
iterator pairs*[A, B](t: Table[A, B]): (A, B)
iterator pairs*(t: StringTableRef): tuple[key, value: string]