axelson

axelson

Scenic Core Team

Documentation of what an "MFA" is?

Is there anywhere that “MFA” is described in detail in the official Elixir documentation? I’ve tried to find it before but was unable to. Am I just missing it somewhere?

(I know what it is I just to have somewhere to link to in my own documentation for anyone unfamiliar with the concept).

Most Liked

peerreynders

peerreynders

In context of typespecs:

mfa()	{module(), atom(), arity()}

For child_spec() Erlang uses mfargs() to refer to a {M,F,A} tuple (module, function atom, arguments list - Kernel.apply/3, :erlang.apply/3).

13
Post #2
kip

kip

ex_cldr Core Team

Its a good question! The nearest I could find was an erlang type definition in the docs at http://erlang.org/doc/reference_manual/typespec.html

tfwright

tfwright

This has recently come up for me as I am working on a library where I am adding support for a “function reference” argument. Previously I was expecting {module, function, args} even though the args component was not really used for anything simply because that format seems to be universal in Elixir. But due to a behavior change I really need to know what arity to expect and I’m sort of at a loss when it comes to best practice!

I am using NimbleOptions and there is no built in support for this. In fact, the documentation has this line which was puzzling to me right off the bat:

:mfa - A named function in the format {module, function, arity} where arity is a list of arguments. For example, {MyModule, :my_fun, [arg1, arg2]}.

(There is also {:fun, arity} but in my use case the module is also variable, edit: and also on closer reading this appears to be for functions of set arity?)

I have to wonder why it seems that in Elixir mfa has come to mean specifically the args variation and I don’t think I’ve seen a mfargs type in the wild…

voughtdq

voughtdq

Did a little code excavation and it turns out that, even in the Erlang repository “mfa” can mean both, but if in the “args” variant, I see a lot more adhoc type specs. It’s probably one of those cases where its interchangeable use has caused it to have multiple meanings. It does help that you can match on the third element for the various cases:

  • {m, f, a} when is_integer(a) and a >= 0 - known arity
  • {m, f, :_} - unknown arity
  • {m, f, a} when is_list(a) - args instead of arity

I think capturing &SomeModule.function/2 is more familiar, but {SomeModule, :function, 2} does make it easier to check if the function exists (vs having to check the capture with Function.info/2).

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement