tushar
The length(List) function can be used in Guards if we want to have different methods based on the length of a list , but can we use the length function for argument pattern matching of a function ?
I guess no we cant but still would love to have an expert opinion from here ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wolf4earth
Short answer: No.
Long answer: How would you expect this to work? I think some pseudo code would be helpful to illustrate your thoughts.
tushar
Something of this sort
where list will be the argument to the method , this can save some space as we will have to write less guards to bring in polymorphism.
Schultzer
You could write a macro and use Kernel.SpecialForms — Elixir v1.20.2
Schultzer
That looks really bad, if this is your intention you should use a guard.
tushar
I have used a guard but again if we are comparing in guards and we have pattern matching then we might think of combining the both and come up with something like this which is more compact and sleek.
Agreed its not a part of the language features so far but if it was it would be great
wolf4earth
To be honest I fail to see how this is more compact and sleek.
With patterns one matches on the structure of the data. It’s explicit and describes exactly how the data should look like. Guards exist to enforce further constraints which cannot be easily expressed in a structural pattern, such as a value being an integer.
That said, your particular examples can be expressed using structural patterns (and I would argue more clearly):
tushar
Yeah I agree , this can be achieved this way too but I was talking more from a good to have perspective.
Just to clear up my own thought process ,
Do you see a problem if the comparison is allowed at the argument level ?
wolf4earth
It mixes concerns.
On the one hand you have structural matching using patterns and on the other hand you have logical matching using conditions (guards). Both are useful but serve subtly different purposes.
Mixing them is not necessarily a bad thing but from where I’m standing it’s good to know that a pattern is a pattern and a guard is a guard. I can look at a function header and know that there is no logical component to the match (such as an
or) as long as there isn’t a guard clause.dimitarvp
I still don’t get what kind of polymorphism you are after. If you explain your intended use-case with a more complete code sample then I’m sure we can give you a proper advice.
Outside of that I am with @wolf4earth – don’t mix guards with pattern matching, they serve different purposes. You can use both at the same time though and that’s perfectly valid if you want stricter checks.