If I have a script called tracer.nim and let's say I am using it to trace messages and I import the tracer into my main.nim script and use it with a public proc like => tracer.writeMessage("some message")
Now if I have another script called script2.nim and I want to use the tracer there as well but I want to pass it into a proc as a param so that I get exactly the instance already used in main.nim. Is that possible?
script2.nim what I would like to achieve =======
var first_tracer:whatsTheType = nil #nil not allowd here
proc init *(tracer:whatsthetype):void =
first_tracer = tracer
proc *useTheFirstTracerInstance():void =
first_tracer.writeMessage("message")
Thanks
If I'm understanding correctly, you are thinking that modules in Nim can be instantiated more than once?
That's not how it works, there's only one instance of each module. If you import it twice, you get access to the same copies of the same variables.
e.g.
# a.nim
var n = 0
proc foo*() =
echo "You have called foo " & $n & " times."
inc n
# b.nim
import a
echo "beginning of b"
foo()
echo "end of b"
# main.nim
import a
import b
foo()
foo()
If you compile & run main.nim, you get:
beginning of b
You have called foo 0 times.
end of b
You have called foo 1 times.
You have called foo 2 times.
Note that a.nim is imported in 2 different modules, yet foo() still prints how many times it was called from any module.