Code below throws C-lang error, seems like a bug?
The example is a broken (to make example simpler) implementation of the argmin function.
import sugar, sequtils
func findi_min*[T](list: openarray[T], op: (T) -> float): int =
list.map((v) => (op(v), 0))[0][1]
proc find_something(v: int, list: seq[int]): int =
list.findi_min((r) => (r - v).abs)
echo find_something(1, @[1])
Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
Hint: used config file '/playground/nim/config/config.nims' [Conf]
.........
Hint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_io.nim.c.o /usercode/nimcache/stdlib_io.nim.c [Exec]
Hint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_system.nim.c.o /usercode/nimcache/stdlib_system.nim.c [Exec]
Hint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_sequtils.nim.c.o /usercode/nimcache/stdlib_sequtils.nim.c [Exec]
Hint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c [Exec]
/usercode/nimcache/@min.nim.c: In function 'find_something__Sy1A5qO72g9aM0AG1s9c03Iw':
/usercode/nimcache/@min.nim.c:551:86: error: incompatible type for argument 3 of 'findi_min__21HJ9aws2bHQ5Ulk4MVcpCQ'
result = findi_min__21HJ9aws2bHQ5Ulk4MVcpCQ(list->data, (list ? list->Sup.len : 0), T1_);
^~~
In file included from /usercode/nimcache/@min.nim.c:11:0:
/usercode/nimcache/@min.nim.c:498:29: note: expected 'tyProc__1un7Zt9bB5WVQaIdXFZeiNQ {aka struct <anonymous>}' but argument is of type 'tyProc__vwnqUR9cTZ8xwFKGcxd2hkw {aka struct <anonymous>}'
N_LIB_PRIVATE N_NIMCALL(NI, findi_min__21HJ9aws2bHQ5Ulk4MVcpCQ)(NI* list, NI listLen_0, tyProc__1un7Zt9bB5WVQaIdXFZeiNQ op) {
^
/playground/nim/lib/nimbase.h:252:44: note: in definition of macro 'N_NIMCALL'
# define N_NIMCALL(rettype, name) rettype name /* no modifier */
^~~~
Error: execution of an external program failed: 'gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c'
It seems like one, because replacing (r) => (r - v).abs with proc(r: int): float = float abs(r - v) solves the issue.
My guess that it is related to proc signature inference - you require T -> float, but r => abs(r - v) by itself is int -> int, but your proc expects int -> float
Thanks, seems like the code below is also a bug, as it compiles and runs, but it shouldn't.
As there's an error - the return of the (r) => (r - 1) expression is int, but the findi_min expect a proc with the float as return type, seems like it shouldn't compile...
import sugar, sequtils
func findi_min*[T](list: openarray[T], op: (T) -> float): int =
0
echo @[1].findi_min((r) => (r - 1))