Hi there,
I have a scoping issues, and did not manage to find a solution on my own. I was hoping that someone more knowledgeable might have a quick solution for this:
- I define an abstract type
AbstractModel
in a module, and a functionfunc1
that works with a sub-type of it. - After loading this module, I define a subtype
Model <: AbstractModel
. I cannot define this in the module, as I dont know all fields beforehand. - The task of
func1
is to output the correct name ofModel
- here is my problem.Model
is not known when I load the module, and I get an error. MWE:
module TestModule
## Define Abstract type
abstract type AbstractModel end
## What I would Like - Define function that works with Model<:AbstractModel
function func1(mod::M) where {M<:AbstractModel}
eval( nameof( typeof(mod) ) )
end
## Export everything
export AbstractModel
export func1
end
using Main.TestModule
struct Model{A,B} <: AbstractModel
a :: A
b :: B
end
mod = Model(1., 2)
func1(mod) #UndefVarError: Model not defined - not defined in Module yet
eval( nameof( typeof(mod) ) ) #But easy to do in global scope...
Unfortunately, I cannot just use eval( typeof(mod) )
(which would work), and I need the struct name, as I may need to initiate a new Model
with different field types (Dual numbers instead of Floats). If I define
function func2(mod::M) where {M<:AbstractModel}
eval( typeof(mod) )
end
in the module, I actually get a valid result func2(mod) #Model{Float64, Int64}
. However, I need the name of the struct, not including all the types. Is there any way that I could func1
get to work?
Best regards,
Edit: Alternatively one could easily just get the Symbol :Model
, but then it costs much more to initiate such a struct because I have to work with eval(:Model)
every time.