Rich_Morin

Rich_Morin

Why doesn't Elixir check the arity of anonymous function calls?

I recently detected a bug (thanks, Dialyzer!) where I was calling an anonymous function with the wrong number of arguments. What confuses me is the fact that the Elixir compiler didn’t complain. Here is a sample which demonstrates the issue:

defp foo(c) do
  bar_fn = fn a, b -> a + b end

  bar_fn.(c)
end

Is there some reason why this check is not being made?

Most Liked

ericmj

ericmj

Elixir Core Team

Elixir is dynamically typed which means that the code analysis the compiler does is limited. The same issue is can be seen with code such as x = :abc; x + 1. A statically type checked language would find issues like this.

NobbZ

NobbZ

Because the compiler doesn’t know that bar_fns type is a function at all. How should it then know and check it’s arity?

Try it out and assign 4, the compiler will still not complain.

NobbZ

NobbZ

As I said, the compiler doesn’t know about types.

f = 4
f.(1)

This would compile as well.

dimitarvp

dimitarvp

One way you could get around this is to pass the anonymous function to another function and put a typespec in place that asserts on the anonymous function’s signature:

defmodule Demo do
  @spec do_stuff(atom(), (integer(), integer() -> integer())) :: atom()
  def do_stuff(key, anon_fn) when is_atom(key) and is_function(anon_fn) do
    anon_fn.(2, 3)
    :ok
  end

  @spec call_do_stuff() :: atom()
  def call_do_stuff() do
    anon_fn = fn a, b -> a + b end
    do_stuff(:hello, anon_fn)
  end
end

This is just quickly scribbled without compiling but at least that way you can rely on Dialyzer, part of the time.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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

Other popular topics 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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement