# Trouble understanding macros: creating a struct on the fly

**URL:** <https://discourse.julialang.org/t/trouble-understanding-macros-creating-a-struct-on-the-fly/4576>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [June 30, 2017, 9:34pm UTC](https://discourse.julialang.org/t/trouble-understanding-macros-creating-a-struct-on-the-fly/4576 "2017-06-30T21:34:41Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![JonathanAnderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathananderson/32/890_2.png) [@JonathanAnderson](https://discourse.julialang.org/u/JonathanAnderson)\
**Post date:** [July 2, 2017, 3:48pm UTC](https://discourse.julialang.org/t/trouble-understanding-macros-creating-a-struct-on-the-fly/4576/6 "2017-07-02T15:48:46Z")

</div>

So I ended up writing the following functions to make my datatype and to slurp in my array of datatypes from disk.

```julia
function make_schema_struct_expr(dataset::String)
    function schema_helper(element)
        :($(Symbol(element[1]))::$(Symbol(titlecase(element[2]))))
    end
    schema=open(JSON.parse, "/path/to/data/$dataset/schema.json", "r")
    Expr(:type, false
        , Symbol(dataset)
        , Expr(:block, map(schema_helper, schema)...))
end
    
function load_dataset{T}(dataset::Type{T}, datafile_name)
    filepath = "/path/to/data/$dataset/$datafile_name"
    num_records::Integer = stat(filepath).size / sizeof(dataset)
    open(f->read(f, dataset, num_records), filepath, "r")
end

```

and then I just make sure I eval the struct and then load in my data.

```julia
eval(make_schema_struct_expr("MyDataset"))
dat = load_dataset(MyDataset, "file_0.dat")

```

I bet I can do this even more cleanly but this feels like a win. Thank you @cstjean for your help! =D

---

_[View the full topic](https://discourse.julialang.org/t/trouble-understanding-macros-creating-a-struct-on-the-fly/4576)._
