I would like to define in an automatic way a struct like:
struct MyStruct
some_field
end
where the struct name “MyStruct” can be read from a string array. I found https://github.com/cstjean/QuickTypes.jl, but from my understanding this only uses parameterization for the struct fields.
Many thanks for helping me! @Daniel_Berge, I want to use multiple dispatch in order to call certain methods based on the argument type. To be more specific, I want to write some generic high level wrappers for lab instruments. For example:
function set_voltage(psu::Agilent, volt = 0)
write(psu.obj, "SpecificAgilentCommand")
end
function set_voltage(psu::Tek, volt = 0)
write(psu.obj, "SpecificTekCommand")
end
I know this could be handled easily with if/else, but I want to try a different way.
“psu” can contain fields like:
struct psu
name
address
obj
end
and the “obj” field can be initialized using GitHub - BBN-Q/Instruments.jl: Instrument control in Julia and the “address” field.
Having multiple types of equipment it becomes cumbersome to hardcode manually all the structs with instruments names, so I wanted a way to auto-generate a struct based on the instrument that I need, in order to specialize a method.
I think code is much more readable like this and anyone can add a new method for a different instrument easily.
This does not seem to work (Version 1.3.0) (Edit: yes it does, fixed a typo!):
julia> struct Inst{Kind}
name
address
obj
end
julia> a=Inst{:Agilent}("foo",2,3)
Inst{:Agilent}("foo", 2, 3)
julia> f(i::Inst{:Agilent}) = 1
f (generic function with 1 method)
julia> f(a)
1
@mauro3, your last solution is even simpler! Thank you! @KajWiik, I think there is a typo in your code(Agilent vs Aglient). Your example worked for me in 1.3.1