Proposal: allow to omit actual arguments to functions when they have default values

31 views
Skip to first unread message

Mário Guimarães

unread,
Jul 9, 2019, 3:30:52 PM7/9/19
to elixir-lang-core
Hi,

when a function is defined like

def fun(a, b \\ 1, c \\ 2)

there is no way to call it by omitting the second argument, that is, one must call

fun(a, 1, c)

The idea of this proposal is to support the calling idiom

`fun(a, _, c)`

as the equivalent of calling

fun(a, 1, c)

That is, in a function call the underscore would mean "use the default value for the argument at this position".

I remembered that this could be an interesting syntax addition to Elixir, hence this post.

I don't know the implications or difficulty of implementing this, besides seeing this as a non-breaking change proposal.

Best regards,
Mário

José Valim

unread,
Jul 9, 2019, 4:17:49 PM7/9/19
to elixir-l...@googlegroups.com
You haven't elaborated on why this would be a good feature, which problems it solves, and what are the alternatives.

In any case, the issue with this feature is that it couples the caller with an implementation choice of the callee. Today, if use default arguments and for some reason I want to stop doing so, I can freely do it without breaking user code. If we add this syntax, the default arguments become official part of the function API, which makes it harder for code to change in the future. It looks to me like very bad coupling.

If you have multiple default arguments and you want to skip one of them, as shown in the proposal, odds are that you should be using options / keyword lists instead (i.e. named arguments instead of positional ones).

José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/1f77d478-247b-4071-b55f-9d14e0140a82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mário Guimarães

unread,
Jul 9, 2019, 4:50:46 PM7/9/19
to elixir-lang-core
This one seems a strong argument against this proposal or other alike:

> Today, if use default arguments and for some reason I want to stop doing so, I can freely do it without breaking user code.

Thanks


terça-feira, 9 de Julho de 2019 às 21:17:49 UTC+1, José Valim escreveu:
You haven't elaborated on why this would be a good feature, which problems it solves, and what are the alternatives.

In any case, the issue with this feature is that it couples the caller with an implementation choice of the callee. Today, if use default arguments and for some reason I want to stop doing so, I can freely do it without breaking user code. If we add this syntax, the default arguments become official part of the function API, which makes it harder for code to change in the future. It looks to me like very bad coupling.

If you have multiple default arguments and you want to skip one of them, as shown in the proposal, odds are that you should be using options / keyword lists instead (i.e. named arguments instead of positional ones).

José Valim
Skype: jv.ptec
Founder and Director of R&D


On Tue, Jul 9, 2019 at 9:30 PM Mário Guimarães <mario.lui...@gmail.com> wrote:
Hi,

when a function is defined like

def fun(a, b \\ 1, c \\ 2)

there is no way to call it by omitting the second argument, that is, one must call

fun(a, 1, c)

The idea of this proposal is to support the calling idiom

`fun(a, _, c)`

as the equivalent of calling

fun(a, 1, c)

That is, in a function call the underscore would mean "use the default value for the argument at this position".

I remembered that this could be an interesting syntax addition to Elixir, hence this post.

I don't know the implications or difficulty of implementing this, besides seeing this as a non-breaking change proposal.

Best regards,
Mário

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-l...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages