Is there a way to define a proc inside a file that is called from an imported module. My idea is that I will have a library for doing common operations on an object that has a generic type associated with it. Then a user could define a desired behavior for some custom type that is triggered during one of these common operations.
In the module I have the default behavior which is to do nothing when there is no associated generic type.
# module.nim
type
Object[T] = object
data: T # optional type parameter
proc second(obj: Object[void]) =
discard
proc first[T](obj: Object[T]) =
second(obj)
If this module is imported could I define another second() proc for some other type such as a string as a simple example?
import mod
proc second(o: Object[string]) =
echo o.data
var
obj = Object[string](data: "Data")
first(obj)
I have a related but less pressing question. What does the compiler end up doing with a proc like:
proc second(obj: Object[void]) =
discard
Would this be expected to have any additional overhead at runtime?proc first*[T](obj: Object[T]) =
mixin second
second(obj)
mixin makes second an open symbol choice, which means in english "this procedure will mix in any procedure named second at instantiation" which allows you to define these sort of generic interfaces.
Would this be expected to have any additional overhead at runtime?
Nim is statically typed and proc is statically dispatched so if you do not see any dispatch logic there is no runtime cost to the dispatch. method is dynamically dispatched and has a runtime cost.
Great! Thank you!
I thought mixin was how to go about this. It wasn't clear to me from the manual how I should apply it.