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

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement