Is it possible to have an `is_module` function?

504 views
Skip to first unread message

Devon Estes

unread,
Oct 16, 2017, 2:02:16 PM10/16/17
to elixir-lang-core
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?

Martin Svalin

unread,
Oct 16, 2017, 3:10:51 PM10/16/17
to elixir-lang-core
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 <devon....@gmail.com>:
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8a823a05-3da0-4de8-b96c-273bd12297b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Allen Madsen

unread,
Oct 16, 2017, 3:34:35 PM10/16/17
to elixir-l...@googlegroups.com
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.
On Mon, Oct 16, 2017 at 3:10 PM, Martin Svalin <martin...@gmail.com> 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 <devon....@gmail.com>:
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 elixir-lang-core+unsubscribe@googlegroups.com.

--
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-core+unsubscribe@googlegroups.com.
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.

José Valim

unread,
Oct 16, 2017, 5:05:46 PM10/16/17
to elixir-l...@googlegroups.com
function_exported?/3 returns false if the module is not loaded (i.e. it does not load the module). Code.ensure_loaded?/1 should be preferred but it is a very expensive operation to be allowed in guard.

The is_atom check is the way to go.



José Valim
Founder and 
Director of R&D

Reply all
Reply to author
Forward
0 new messages