OvermindDL1, `function_exported?` was the second method I tried, but the caveat of it needing to be loaded in memory causes too many issues IMO. That is why I settled on the current version I linked to above, which is a more direct variant of what you are suggesting.
On Mon, Feb 26, 2018 at 5:13 PM, OvermindDL1 <[email protected]> wrote: > For note, for a quick-check if a module exists I just do something like: > > ```elixir > ╰─➤ iex > Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] > [async-threads:10] [hipe] [kernel-poll:false] > > Interactive Elixir (1.6.1) - press Ctrl+C to exit (type h() ENTER for > help) > iex(1)> function_exported?(String, :module_info, 0) > true > iex(2)> function_exported?(SomeModule, :module_info, 0) > false > ``` > > This *only* works on modules already loaded in memory (which the > afore-mentioned `Code.ensure_loaded/1` and `Code.ensure_compiled/1` do in > various ways), but in production running in embedded mode that's fine (it's > always the case for me, but be aware of your cases). > The reason this works as that the BEAM VM requires a `module_info/0` > function on the BEAM module as it uses it for various things, so it's easy > just to test for the existence of this function. > > You can always try calling `blahmodule.module_info()` and catch the > exception if it doesn't exist, this should force loading so should be > faster then the `Code.*` calls but still not as fast as > `function_exported?` though. > > On Saturday, February 24, 2018 at 7:49:40 PM UTC-7, [email protected] > wrote: >> >> It is more difficult than you would expect to check if something in >> Elixir is a module. I think it is fairly reasonable to build this >> functionality into the language. The best way I've found to do this is to >> call a function on the value and rescue from the `UndefinedFunctionError`. >> This feels a bit hacky to me and I think there should be a more elegant >> solution. >> > -- > 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/T3hCNAKmrds/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/elixir-lang-core/f0514cb7-18d1-4d73-800e- > 35b56d4322d3%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/f0514cb7-18d1-4d73-800e-35b56d4322d3%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CANhOUugaiV40Hcr7UGb_KzD_9JJzDVUVK2_UhSB4TXSKBcog5Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
