spammy
Default Parameters and Private Arities
Say we have a function:
def func(a, b \\ 0) do
I believe this generates func/1 and func/2. Is it possible to make func/2 private without spinning out a helper/implementation/do_func/2 equivalent?
Most Liked
sodapopcan
I’ve always found it helpful to think of \\ as a simple convenience rather than “default arguments.” All it’s doing is turning this:
def bar(a, b \\ 0) do
a + b
end
into this:
def bar(a) do
bar(a, 0)
end
def bar(a, b) do
a + b
end
The easiest and clearest way to get what you want it to just write it out manually as suggested.
So the short answer is no, but technically yes if you consider redefining def and changing how \\ works ![]()
4
al2o3cr
You can mix def and defp with different arities:
defmodule Foo do
def bar(arg), do: bar(arg, 42)
defp bar(arg, arg2), do: {arg, arg2}
end
iex(6)> Foo.bar(1)
{1, 42}
iex(7)> Foo.bar(1, 2)
** (UndefinedFunctionError) function Foo.bar/2 is undefined or private. Did you mean:
* bar/1
Foo.bar(1, 2)
iex:7: (file)
1
LostKobrakai
I’d say yes. It’s just accidental that the private one happens to have a different arity, which allows for it being named the same.
1
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
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
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
I would like to know what is the best IDE for elixir development?
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Other popular topics
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
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New







