Is there any way to produce a Type by cat strings together?

To define a set of Regxx types,It would be OK with these pieces of code:

for (bits,t) in [(8,UInt8),(16,UInt16),(32,UInt32)]
    rname = Symbol("Reg",bits)
    @eval struct $rname
        addr::UInt32
        data::$t
    end
end

But I wonder if there is any way to define types like this:

for bits in [8,16,32]
    name = Symbol("D",bits)
    @eval struct $name
        addr::UInt32
        data:: ??? # needs to produce UInt8、UInt16、UInt32
    end
end

I think you have already posted the answer:

for bits in [8,16,32]
    name = Symbol("D",bits)
    t = Symbol("UInt", bits)
    @eval struct $name
        addr::UInt32
        data::$t
    end
end
3 Likes

You are right! :joy: :joy: :joy:

I ran into a error when I tried the code like yours, excepted a statement:

for bits in [8,16,32]
    rname = Symbol("D",bits)
    @eval struct $rname
        addr::UInt32
        data::Symbol("UInt",$bits)  # error!  due to not understand @eval's usage
    end
end

Thanks a lot !

1 Like