hubertlepicki

hubertlepicki

So I want to check if a module exist and has some function defined. For example, I want to check if I have module Foo with foo/2 function defined. Any ideas?

Showing Posts 1 to 9

hubertlepicki

hubertlepicki OP

Looks like I can check for module existence this way:

Code.ensure_loaded(String)
{:module, String}

hubertlepicki

hubertlepicki OP

Yes! Thank you @dgamidov seems like I can do

Foo.module_info(:exports)

that returns list of function names with arity. Perfect.

sashaafm

sashaafm

@hubertlepicki Code.ensure_loaded/1 doesn’t check if it exists. It checks if it is loaded and if not then it is loaded (or tries to load it):

Ensures the given module is loaded.

If the module is already loaded, this works as no-op. If the module was not yet loaded, it tries to load it.

Qqwy

Qqwy

TypeCheck Core Team

For posterity: It is more Elixir-like to use Foo.__info__(:functions).

:functions is an alias of the Erlang-specific :exports, but a lot clearer. Also, __info__/1 is the Elixir-wrapper around :erlang.module_info/1 and allows slightly more atoms as input (such as :macros).

See the docs for more information.

Also note that there are many functions inside the stdlib Module module , but most (except for __info__/1) only work during compile-time.

rvirding

rvirding

Creator of Erlang

Do you want to test if the module is loaded and exports a specific function, or you do you want to test if there exists a module which you can reach at all containing the function? If it is the first then you have the function :erlang.function_exported(module, function, arity). I don’t know if there is an Elixir interface function. The other alternative has already been covered here, but be aware that the solutions, when they check, will load the module if it not already loaded.

hubertlepicki

hubertlepicki OP

yup, I am good with loading module if it hasn’t been loaded at all. I want to know if I can call this Module.function. If I load it doing that, it’s also ok.

Qqwy

Qqwy

TypeCheck Core Team

Another thing that you might like to look at, are Behaviours. If your current use-case is to have later-defined modules (e.g. made by users of your library) that have to follow a fixed specification you provide, then Behaviours are the way to go.

kuroda

kuroda

Elixir has the function Kernel.function_exported?/3, which returns true if the speficied module is loaded and contains a public function with the given arity, otherwise false.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews