This is the best way I know how to determine if it's a module vs an atom. function_exported?(X, :__info__, 1)
You won't be able to use it in a guard though. Allen Madsen http://www.allenmadsen.com On Mon, Oct 16, 2017 at 3:10 PM, Martin Svalin <[email protected]> wrote: > The closest thing we have is `Code.ensure_loaded/1`. If you don't check > that the module exists, you're basically just checking that the input is an > atom. And if you use ensure_loaded, you'll have a predicate function with > side effects. You'd also break the expectation of guard safety with the > `is_` prefix. > > Would your `is_module/1` have different behaviour than `is_atom/1`? Would > it be true only of atoms of the form `:"Elixir.SomeModule"`? I'm not sure > it should, since `defmodule :random_atom` is valid. > > Which is a long way of saying, I think you should use `is_atom`. Perhaps > with a line comment `# for modules`. > > > mån 16 okt. 2017 kl 20:02 skrev Devon Estes <[email protected]>: > >> We just encountered something that seemed kind of odd, so I wanted to >> pass it by the list to see if it could be a possible feature. >> >> In Benchee we want to accept either a module or a function as an argument >> to a function. We'll have different behavior depending on the type there >> (since we're expecting any module passed in to implement a certain >> behaviour). However, there's no check for `is_module`. We were thinking of >> using `is_atom` (see here: https://github.com/PragTob/ >> benchee/pull/148/files#diff-e8ef07ff6be58cfaaaf6f03420fc53e7R75), but >> that seems sort of confusing to me. I know technically modules are atoms, >> but would it be possible to have an `is_module` function that can be used >> in guard clauses and elsewhere that would more accurately convey the sort >> of thing we're trying to do? >> >> -- >> 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/8a823a05-3da0-4de8-b96c- >> 273bd12297b8%40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-core/8a823a05-3da0-4de8-b96c-273bd12297b8%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/CAAHw6CKCXb0Q67YMXFF0_ > fdcUaLvXWnAz5mLHEGFCZB%2BrU_J_Q%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/CAAHw6CKCXb0Q67YMXFF0_fdcUaLvXWnAz5mLHEGFCZB%2BrU_J_Q%40mail.gmail.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/CAK-y3Ctff5hfTWP7kQ8skvuoONNrCZ7Vj1eW0ZtibAzdEhOBHg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
