# Metaprogamming

**URL:** <https://discourse.julialang.org/t/metaprogamming/42286>\
**Category:** General Usage\
**Tags:** question, metaprogramming\
**Created:** [June 30, 2020, 5:51am UTC](https://discourse.julialang.org/t/metaprogamming/42286 "2020-06-30T05:51:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [June 30, 2020, 5:51am UTC](https://discourse.julialang.org/t/metaprogamming/42286/1 "2020-06-30T05:51:14Z")

</div>

Hi there,

I am writing a module with dozens of `MyType` like structure (as below), that all belong to an Abstract super type. I would like to be able to write a meta constructor (roughly sketched below). Is it possible?

```julia
struct MyType <: SomeAbstractType
    field1
    field2
    field3
    other::Dict
end

function MyType(d::Dict{AbstractString,AbstractString})
    var1 = f(d, "field1")
    var2 = f(d, "field2")
    var3 = f(d, "field3")

    MyType(var1, var2, var3, d)
end

function meta_constructor(d::Dict{AbstractString,AbstractString}, input::T)
     where T <: SomeAbstractType

    mytuple = for fields in fieldnames(T) ...

    T(mytuple)
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 30, 2020, 6:09am UTC](https://discourse.julialang.org/t/metaprogamming/42286/2 "2020-06-30T06:09:53Z")

</div>

Yes, that is possible. However, if you’re constructing from a dict, which will be type-unstable and not super performant, I don’t see a need to do it. I don’t think you gain anything from your current solution.

---

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [June 30, 2020, 6:10am UTC](https://discourse.julialang.org/t/metaprogamming/42286/3 "2020-06-30T06:10:59Z")

</div>

A lot of lines actually … roughly 1k … and I am a bit lazy

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 30, 2020, 6:24am UTC](https://discourse.julialang.org/t/metaprogamming/42286/4 "2020-06-30T06:24:26Z")

</div>

Does this not work:

```julia
function meta_constructor(d::Dict{AbstractString,AbstractString}, input::T)
     where T <: SomeAbstractType
    T((d[string(fld)] for fld in fieldnames(T))...)
end

```

?  
(not sure what the `f(d, "field1")` is about, but this could be added too)

---

<div class="post-metadata">

**Author:** ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)\
**Post date:** [June 30, 2020, 6:31am UTC](https://discourse.julialang.org/t/metaprogamming/42286/5 "2020-06-30T06:31:18Z")

</div>

Thanks, I’m going to try it!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 30, 2020, 6:38am UTC](https://discourse.julialang.org/t/metaprogamming/42286/6 "2020-06-30T06:38:50Z")

</div>

Corrected an error in the example. Probably you’re aware of this:  
Also if you use a `Dict{Symbol, AbstractString}` then you can drop the `string` conversion. Last, if you can use a `Dict{Symbol, String}` and uses fields `field1::String`, you might speed things up.
