I have a library cool_lib.nim where I have a few types with a proc, let's say:
proc abs*(x: MyType): float = ....
proc abs*(x: MyType2): float = ....
And they are used in a generic proc:
proc coolProc*[T](x: T): T =
....
let a = abs(x)
....
Now I want to let my user pass their own types (with a abs proc) to coolProc like this:
import cool_lib
type CoolType = object ....
proc abs(x: CoolType): float = ....
var coolVar = CoolType(...)
coolProc(coolVar)
How can I access the abs proc of CoolType from my library? I guess I could just include cool_lib instead but that seems like a workaround. Is there a reasonably easy way to do it with imports?Maybe i do not understand correctly but for me it "just works"
coolLib.nim
proc abs*(x: int): float =
return 0.1
proc abs*(x: float): float =
return 0.2
proc coolProc*[T](x: T): T =
let a = abs(x)
echo a
coolMain.nim
import coolLib
type CoolType = object
proc abs(x: CoolType): float =
return 0.3
var coolVar = CoolType()
echo coolProc(coolVar)
outputs:
Hint: /home/david/nimPlayground/coolMain [Exec]
0.3
()