Macro does not like module attribute?

Thank you both!

Let me preface this with the fact that I can live with adding an extra variable on top-level by hand if that’s easiest.

But I still want to follow up with you.

@josevalim - I tried your solution, and it works for the module atttribute case.

Another use case then has a different problem:

defstruct_module(Channel.Quality, [<... some fields ...>])
[...]
defstruct_module(Model.Params,   [channel:         %Channel.Quality{}])

Will result in:

== Compilation error in file lib/model.ex ==
** (CompileError) lib/model.ex:20: cannot access struct Channel.Quality, the struct was not yet defined or the struct is being accessed in the same context that defines it
    expanding struct: Channel.Quality.__struct__/1
    lib/model.ex:20: (file)
    expanding macro: Utils.Struct.defstruct_module/2
    lib/model.ex:20: (file)

I assume this is due to the fields list being expanded now outside the defmodule and compiled before that “inner module” was compiled yet?

The original version worked with that scenario where the list was given directly.

I tried bind_quoted but then I also need to use it on struct_name field and then aliases are expanded differently (and no longer include the name of any module defined one level higher).

It’s a tricky one… There’s always a scope where it doesn’t come together.

@DaAnalyst -

I know it’s on me / is my fault, but I’m looking at the diff of your project and I just can’t see how that pertains to my problem. My mind doesn’t make the connection.