axelson

axelson

Scenic Core Team

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).

Showing Posts 1 to 7

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 #1
kip

kip

ex_cldr Core Team

Its a good question! The nearest I could find was an erlang type definition in the docs at Types and Function Specifications — Erlang System Documentation v29.0.2

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

Do you mean for enforcing that it has a specific arity or supporting functions that have unknown arities? Not sure about best practices as I prefer to support them all as a matter of flexibility.

For unknown, the {SomeModule, :function, args} is what I would reach for. I rarely use it in practice though:

def call_mfa(a, b, {module, function, args}),
  do: apply(module, function, [a, b | args])

Then all the others -

  • {SomeModule, :function}
  • &SomeModule.function/2
  • fn _a, _b -> :ok end

For expected arity, it’s just the others (i.e. no {m, f, a}).

Not sure about that option for NimbleOptions, but I think it can take a capture like &Enum.map/2, which would cover the bases of requiring a specific arity. Then it’s just a matter of taste on whether you want to support {SomeModule, :function}.

Edit:

Yeah I was actually surprised to see it called arity because that would imply it should be {MyModule, :my_fun, 2}, right? It would be interesting to know the historical context of calling the third element arity.

tfwright

tfwright

Both, I think. Possibly I am missing some angle but in my use case I intended to require the user specify a function, which requires specifying an arity, without making any assumptions about the arguments.

I suppose I could allow them to specify only the function name, but in that case I would have to simply pick the first function defined in the module with that name, which seems like undesirable ambiguity. I could see why in many cases it wouldn’t matter, since the user controls the function and the option, so it can be left to them to pass the correct configuration that matches their implementation, but in my case I can’t infer the desired arity from the option (for reasons I don’t think are relevant here).

Mostly however I am just confused about the mfa/mfargs mismatch between Elixir and Erlang.

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).

soyjeansoy

soyjeansoy

afair I read mfa in typespec section in Elixir hexdocs.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews