omerss2311

omerss2311

Capturing a function with default arguments?

Hey! I have a function defined with default arguments like so:

def my_func(arg1, arg2 \\ %{}, arg3 \\ false)

I want to pass it as a variable, so I’m capturing it as &my_func/3. However, I want to be able to invoke it with only one or two arguments as well, but that gives an error saying “function with arity 3 called with one argument”. Is there a way to do this?

Most Liked

D4no0

D4no0

Default arguments are just a syntactic sugar that transforms into functions with different arity:

def my_func(arg1, arg2 \\ %{}, arg3 \\ false)

# arg2 = %{} and arg3 = false
def my_func(arg1)

# arg3 = false
def my_func(arg1, arg2) 

# no default arguments
def my_func(arg1, arg2, arg3)

So if you choose my_func/3, then you are required to pass all arguments when calling it, if you want to avoid passing all the arguments use either my_func/1 or my_func/2.

LostKobrakai

LostKobrakai

You can look into mfa tuples and apply.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement