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
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Other popular topics
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
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







