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 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/f0514cb7-18d1-4d73-800e-35b56d4322d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to