Structs and "module X is not loaded and could not be found"

701 views
Skip to first unread message

Paul Schoenfelder

unread,
Mar 2, 2014, 12:45:08 AM3/2/14
to elixir-l...@googlegroups.com
Hey guys,

So I've been banging my head against this one for most of today, and I'm hoping folks can take a peek. Josh Adams and I put together a couple quick reproducible cases:

The issue in this case is being triggered by the import:

https://gist.github.com/knewter/590bc70edf8a097f4d3a

===

The issue in this next case is the bigger issue in my opinion:

https://gist.github.com/bitwalker/c4f174a87ecb2cce34cf

Referencing a struct from a module B which references a struct from module A refuses to compile.

Stripping out the struct references, and matching on the arguments as a plain map works fine, but obviously the problem here is that it's desirable to match on the struct. Without that capability, structs lose a great deal of their usefulness from my perspective. I'm sure this is simply a bug that needs to be fixed, but I want to make sure everyone agrees with me and that it's not just me doing something that isn't intended to work in the first place.


Paul

José Valim

unread,
Mar 2, 2014, 3:09:54 AM3/2/14
to elixir-l...@googlegroups.com
So I've been banging my head against this one for most of today, and I'm hoping folks can take a peek. Josh Adams and I put together a couple quick reproducible cases:

The issue in this case is being triggered by the import:

https://gist.github.com/knewter/590bc70edf8a097f4d3a

The error message is clear on this one. The ModuleB is not loaded nor defined by the point we try to import it. Remember Elixir is a lexical language, so the order of definition matters in many cases.

If you split them into files in a project, Elixir is able to figure out and load them in the proper order, but having everything in one file requires the order to be respected.


The issue in this next case is the bigger issue in my opinion:

https://gist.github.com/bitwalker/c4f174a87ecb2cce34cf

Referencing a struct from a module B which references a struct from module A refuses to compile.

Hrm... this is similar to the issue above. ModuleA expects ModuleB to be defined while ModuleB expects ModuleA to be defined. This is a circular dependency at compilation time. 

This happens because structs are expanded at compilation time and it also happens with records. The tricky part is that records were mostly used as data definitions and you didn't have functions in the record module but that's not the case with structs.

I will try to think of a way of making this work with structs. Thanks for the reports!

José Valim

unread,
Mar 2, 2014, 10:27:14 AM3/2/14
to elixir-l...@googlegroups.com
The issue in this next case is the bigger issue in my opinion:

https://gist.github.com/bitwalker/c4f174a87ecb2cce34cf

Referencing a struct from a module B which references a struct from module A refuses to compile.

This use case is now working with structs. We allow mutual dependencies at the struct level because we have made the compiler aware of how structs are defined.

Please give it a try and let me know if you find any further issues!

Paul Schoenfelder

unread,
Mar 2, 2014, 2:08:57 PM3/2/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
So far so good! Like I mentioned in IRC though, the first case was originally split across two files, but it does make sense to me why that one doesn't work, so maybe it's just about making a better error there if possible.

Thanks again!

Paul Schoenfelder

unread,
Mar 2, 2014, 8:37:55 PM3/2/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Hey José,

I've got a follow up for you on this. I've finished porting over Timex entirely to structs, and it compiles fine for me on my machine, runs all the tests, etc. On my wercker box (my continuous integration environment), it fails every single time. Perhaps this has to do with the fact that the CI box is single core, and my computer has 4 cores. In any case, it works locally for me every single time now, but I can't get it to build once on my CI box. I did get this same error a few times during development, but for the last hour or so, I haven't had it fail once, even if I run mix clean/mix compile to force it to start fresh.

Thoughts?

-- 
Paul

From: Paul Schoenfelder Paul Schoenfelder
Reply: elixir-l...@googlegroups.com elixir-l...@googlegroups.com
Date: March 2, 2014 at 1:08:59 PM
To: elixir-l...@googlegroups.com elixir-l...@googlegroups.com
Subject:  Re: [elixir-core:2518] Structs and "module X is not loaded and could not be found"
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/C4ij48DHERQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

José Valim

unread,
Mar 3, 2014, 1:45:34 AM3/3/14
to elixir-l...@googlegroups.com
It is failing because SomeModule.__struct__/0 cannot be found? Can you please send me a link to the code online? And possibly to the build? :)



José Valim
Skype: jv.ptec
Founder and Lead Developer


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages