spacemonk
Hi! I might be asking a silly one, sorry for that and thanks for your time!
my aim is to implement quite simple logic:
the get_or_create_movie could be called with first param with “type” = “movie”, in that case I do not care about any other params and the code stays the same
if the first param contains “type” => “tv-series”, then the logic start to differ according to values. I feel that the straightforward approach I took looks not really pretty ![]()
def get_or_create_movie(%{ "type" => "movie" } = movie) do
# same movie code
end
def get_or_create_movie(%{ "type" => "movie" } = movie, _like_time) do
# same movie code
end
def get_or_create_movie(%{ "type" => "movie" } = movie, _like_time, _review_text) do
# same movie code
end
def get_or_create_movie(%{ "type" => "tv-series" } = series) do
# only series code
end
def get_or_create_movie(%{ "type" => "tv-series" } = series, like_time) do
# like_time only series code
end
def get_or_create_movie(%{ "type" => "tv-series" } = series, like_time, review_text) do
# like_time and review text only series code
end
the dyalizer complains (warning) that the clauses /2 and /3 was previously defined.
the function could be called with “type” = “movie” in the first param and with second and/or third params present
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Shakadak
Hello
Are you certain this is a dialyzer warning ?
It seems to me the problem is about mixing different arity and function clauses.
If you define the same function clause with the same number of arguments together it should be fine, like so:
spacemonk
Ah, right! That is correct. One problem is the repetitive code in “movie”-case. is there a more compact, DRY way to match any arity if the “type” => “movie”?
LostKobrakai
Can you normalize to just /3 functions? E.g. by using a default value if
like_timeorreview_textare not passed.Shakadak
I think @LostKobrakai’s suggestion is the better idea, but if you can’t find a neutral element for
like_timeandreview_textyou can at least use a helper functionPhxie
Not sure what the rest of your code is doing, but is something like the below not possible?
If the like_time/review_text logic is identical for each type, then you can just match on the params as a whole rather than targetting the types specifically. Then handle the “type” within the functions like the below or by using a conditional statement.
spacemonk
I’ve tried that:
one small hicup I had with this is that I can’t pass the function itself as
&get_or_create_movie\3to a Enum stuff as it will raise an exception when it will be passed with one param so I needed to wrap it to anonymous fun and call it with one param like&(Movie.get_or_create_movie(&1))so the defaults would work. But that is okay.after that the Elixir warning complains about clauses and defaults:
which I find as not correct logic for my case, is it?
al2o3cr
Each arity (the number after the
/) is effectively a separate function. You can capture one with the defaults by using a smaller number:(The result for
Enum.scanisn’t terribly relevant, it was just the first function that came to mind that takes a callback that expects two arguments.)In both cases, the arguments that aren’t passed to the lower-arity versions
Foo.bar/1andFoo.bar/2are filled in with the defaults.It’s common in Erlang code to see a long series of function heads with short heads filling in parameters and calling longer ones. This is the same as what Elixir’s compiler builds:
which is why you can capture
bar/1andbar/2distinctly frombar/3spacemonk
thank you for the detailed explanation. That was really eyes opening insight. Now I got the functions much deeper (still a lot to go I believe). elixir community is amazing