I am a beginner and learning about "Generic procs". How Nim finds the correct proc 'min' to call? I was expecting error message: ambiguous proc name 'min' - add module to make it clear which 'min' proc do you mean... Yet - it works just fine.
proc min[T](x, y: T): T =
if x < y: x else: y
var ar1 = [10, 30]
var se1 = @[10,30]
echo min(ar1) # 10
echo min(se1) # 10
echo min(1.1, 3.3) # 10
echo min @[10,30] # < == how Nim knows to call different min proc?
# echo min@[10,30] # is it space after 'min'? Removing space results with "Error: type mismatch"
echo min(10,30) # 10
min generic proc in your example code is never called because system module has non-generic min procs.
Please read:
So, all your min in your example code calling min proc in system module. Don't you got a message "Hint: 'min' is declared but not used" when you compile your code?
Nim's Overloading resolution is explained here: https://nim-lang.org/docs/manual.html#overloading-resolution
https://nim-lang.org/docs/manual.html#overloading-resolution is how nim finds the correct proc.
an exact match is preferred, so your local version of min is never called in any of your examples. instead, in order:
min(ar1) system.min[T](x:openArray[T]):T
min(se1) system.min[T](x:openArray[T]):T
min(1.1,3.3) system.min(x,y: float):float
min @[10,30] system.min[T](x:openArray[T]):T
min(10,30) system.min(x,y: int):int
otherwise a local definition is preferred over one included from another module, iirc, so min('a','c') uses your local min