Proposal: Add Agent.get/1, where Agent.get(agent) is equivalent to Agent.get(agent, fn x -> x end)

28 views
Skip to first unread message

Pierre Le Gall

unread,
May 19, 2020, 9:10:44 AM5/19/20
to elixir-lang-core
Hello,

Writing `Agent.get(agent, fn x -> x end)` or even shorter
`Agent.get(agent, &(&1))` is kind of verbose for this non-specific
behavior.

Maybe the `fun` argument of `Agent.get/2` could be set to `fn x -> x
end` by default making `Agent.get/1` available?

--
Pierre

José Valim

unread,
May 19, 2020, 9:16:15 AM5/19/20
to elixir-l...@googlegroups.com
Getting the whole agent state is often a bad idea, so we want to force you to think if you really need the whole state or part of it. Depending on the data, you can have a drastic difference between:

    Agent.get(pid).some_field

and:

    Agent.get(pid, & &1.some_field)

So in the cases you do want it all, being explicit about it doesn't hurt.

--
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/CAL3hriJqr95fxzLaDXadYsUNWR1-Njzcgtpr-NJp8kS%3Ddin0Jw%40mail.gmail.com.

Pierre Le Gall

unread,
May 19, 2020, 9:36:34 AM5/19/20
to elixir-lang-core
I see the point :)

Thank you for the answer !
Reply all
Reply to author
Forward
0 new messages