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

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement