why are zero arity anonymous functions not allowed?

178 views
Skip to first unread message

Scott Parish

unread,
Jul 20, 2016, 8:01:01 PM7/20/16
to elixir-lang-core
It seems inconsistent that the anonymous function sugar is not allowed for zero arity functions. Is there a good reason for this, or did this need to be proposed as a feature request instead of a bug?


Thanks

Ben Wilson

unread,
Jul 20, 2016, 10:22:21 PM7/20/16
to elixir-lang-core
The title is a bit misleading as it implies that Elixir doesn't have `fn -> "yo" end`, but at least that's cleared up in the body.

I do think it can be useful, IE `Task.async(&foo(args))` is more succinct than `Task.async(fn -> foo(args) end)`. Spawning processes and the like are about the only cases I can think of where zero arity functions are used frequently, but it does seem like it would be a win for consistency and readability in certain cases.

Perhaps there are ambiguities introduced however without &1 and friends? I'm sure one of the core team members will clarify.

Scott Parish

unread,
Jul 20, 2016, 11:29:06 PM7/20/16
to elixir-l...@googlegroups.com
Another one is: :timer.tc(&mycode(args))

Or the case where this originally came up today: retry_num_times(10, &fn_that_may_not_succeed_immediately!(args))

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/HsrPwDjg964/unsubscribe.
To unsubscribe from this group and all its topics, 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/41bfc1d9-fce2-46f1-b72b-5429d0f4fc6b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Scott Parish

unread,
Sep 13, 2016, 12:31:50 PM9/13/16
to elixir-l...@googlegroups.com
Thanks for the details! Makes a lot more sense now.

On Wed, Sep 7, 2016 at 11:58 AM, <cristi...@surhive.com> wrote:
According to https://groups.google.com/forum/#!topic/elixir-lang-core/GeXNvzzmdps, it seems that if you allow zero arity with & there is an ambiguity when parsing the expressions.
&fun/2 could be parsed as (fn -> fun end) / 2 or fn(x,y) -> fun(x,y) end
To unsubscribe from this group and all its topics, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/254f4040-f343-48a0-964e-a8fef7892893%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages