I think I've hit this bug:
https://github.com/Araq/Nim/issues/2007
I'm trying to reference a variable from an outer scope within a closure. Here is my change:
https://github.com/Nycto/AStarNim/commit/afdb2fe285d5d4ef81c24eb5e0181fc08ea7d439
And here is the build failure I'm running in to:
https://travis-ci.org/Nycto/AStarNim
/home/travis/build/Nycto/AStarNim/astar.nim(133, 5)
Error: internal error: expr: var not init :env_164158
Any ideas about how to work around this?
I have no real workaround but I can try to fix this nasty bug.
Obviously preferential :)
In the meantime, if anyone else runs in to this, I fiddled around with various implementations and found a hack that works. If this is the code you are trying to get compiled:
import algorithm
iterator byDistance*[int]( ints: openArray[int], base: int ): int =
var sortable = @ints
sortable.sort do (a, b: int) -> int:
result = cmp( abs(base - a), abs(base - b) )
for val in sortable:
yield val
when isMainModule:
for val in byDistance([2, 3, 5, 1], 3):
echo val
You can use a proc returned from another proc to get it working:
import algorithm
proc buildCmp( base: int ): auto =
return proc (a, b: int): int =
return cmp( abs(base - a), abs(base - b) )
iterator byDistance*[int]( ints: openArray[int], base: int ): int =
var sortable = @ints
sortable.sort( buildCmp(base) )
for val in sortable:
yield val
when isMainModule:
for val in byDistance([2, 3, 5, 1], 3):
echo val
Cheers!