James_E

James_E

Overloaded functions with the same name and arity?

I feel like this function is perfectly ergonomic:

# default value in effect: count \\ :all
@spec delete(t(e), e) :: t(e) when e: term()
@spec delete(t(e), e, :strict) :: t(e) when e: term()

# default value in effect: count \\ :all
@spec delete(t_lax(e), e) :: t_lax(e) when e: term()
@spec delete(t_lax(e), e, :lax) :: t_lax(e) when e: term()

@spec delete(t(e), e, :all | non_neg_integer()) :: t(e) when e: term()
@spec delete(t(e), e, :all | non_neg_integer(), :strict) :: t(e) when e: term()

@spec delete(t_lax(e), e, :all | non_neg_integer()) :: t_lax(e) when e: term()
@spec delete(t_lax(e), e, :all | non_neg_integer(), :lax) :: t_lax(e) when e: term()

So, is there any way to make the rendered exdoc actually reflect that?

I feel like the documentation tooling has produced a hideous, unreadable mess; it looks like it’s somehow crosswise of the overloads and defaults, having completely segregated all the different @spec directives by arity, rather than by semantic group… but I have no clue what I could to do improve it.

Most Liked

martosaur

martosaur

I feel like this is multiple functions in a trench coat

def decrease_strict(ms, element, count)
def decrease_lax(ms, element, count)
def delete_strict(ms, element)
def delete_lax(max, element)
Eiji

Eiji

As far as I know no and I don’t think there would be ever support for that not only in the documentation, but also in Elixir itself. The compiler should warn you that functions with the same name and arity should be grouped together.

You can give different names to all related functions. for example by simply adding _lax suffix and then you can group specific functions. For more information take a look at:
Grouping functions, types, and callbacks | mix docs task @ ex_doc documentation

sodapopcan

sodapopcan

Definitely, it should otherwise this be reported as a bug :sweat_smile:

To be technically correct (the best kind of correct) these are two separate functions: delete/2 and delete/3. When we say that arity is part of the function name, this is very literal. Elixir makes it possible to not group them though this is this more of a quirk of its syntax (and as mentioned it emits a warning and most people compile with --warnings-as-errors). In Erlang this isn’t even possible:

1> foo("A") -> "You said 'A'"; foo(N) -> "You didn't say 'A'".
ok
2> bar("A", "B") -> "You said 'A'"; bar(N) -> "You didn't say 'A'".
* 1:15: syntax error before: '->'

Where Next?

Popular in Questions Top

shahryarjb
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
senggen
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
sergio
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement