Inside a proc I need to do a quick work using a heapqueue. So I make an ad-hoc type for it and define a < proc for it.
But, maybe because the < proc isn't global, the generic instantiation can't find it. Here's an example that shows it:
import std/heapqueue
type G = object
s: string
prio: int
proc `<` (a, b: G): bool = a.prio < b.prio
proc main =
type L = object
s: string
prio: int
proc `<` (a, b: L): bool = a.prio < b.prio
var hg = initHeapQueue[G]()
var hl = initHeapQueue[L]()
hg.push G(s: "x", prio: 1) # ok
hl.push L(s: "x", prio: 1) # compile error
main()
The error:
C:\Users\...\tq.nim(19, 5) template/generic instantiation of `push` from here
C:\Users\...\.choosenim\toolchains\nim-2.0.0\lib\pure\collections\heapqueue.nim(137, 9) template/generic instantiation of `siftup` from here
C:\Users\...\.choosenim\toolchains\nim-2.0.0\lib\pure\collections\heapqueue.nim(89, 15) template/generic instantiation of `heapCmp` from here
C:\Users\...\.choosenim\toolchains\nim-2.0.0\lib\pure\collections\heapqueue.nim(76, 47) Error: type mismatch
Expression: x < y
[1] x: L
[2] y: L
Expected one of (first mismatch at [position]):
[1] proc `<`(a, b: G): bool
[1] proc `<`(x, y: bool): bool
[1] proc `<`(x, y: char): bool
[1] proc `<`(x, y: float): bool
[1] proc `<`(x, y: float32): bool
... (more lines)
How can I make it work? I don't want to make the type and proc global, since it's just a one-off use.