alexiss
In my practice I often write something like this:
with value when not is_nil(value) <- some_fun() do
...
end
What about something like shortcut for this frequently used case?
with non_nil(value) <- some_fun() do
...
end
Unfortunately, I can’t find any way to do it via macroses because as I understand when is a some kind of special form that is handled by compiler.
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
I’m posting this in response to Jose’s recent tweet (Cr. link) :
People are sleeping on Elixir for a coding harness:
Hot-code swappi...
New
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes.
We’re a small ...
New
I’ve just put together a small POC exploring PDF inspection from Elixir/Phoenix:
The idea is pretty simple: drag & drop a PDF in a...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Yeah, that’s because of how
withis implemented, it really should have been a macro itself so it doesn’t have these weird and surprisingly inconsistent edge cases.In short, no, I’ve not found anyway to enhance
withlike that without just flat-out replacing it, which is entirely an option, there are some good libraries that already have.LostKobrakai
It’s exactly the same as it is with
caseas well as in function heads:my_function(not is_nil(x), y)is invalid. I really don’t get what you find so inconsistent withwith?OvermindDL1
It’s expanding into the proper AST node for both
caseandwith, so it should work, it is only because they are special forms not coded to match the rest of the language that they are not. It would obviously not work in a function head because it doesn’t go into a single AST node but rather would have to be split out into two calls instead (I.E.def my_function(x when not is_nil(x), y)makes no sense, butcase ... do x when not is_nil(x) -> ... enddoes make sense, and yet you cannot replace it with a macro ofcase ... do non_nil(x) -> ... endeven ifnon_nil(x)is just as trivial asdefmacro non_nil(v), do: quote(do: unquote(v) when not is_nil(unquote(v)))even though it is AST-identical, thus inconsistent, and even more so withwithbecause it is a weird magical multi-arity function along withforunlike everything else in the language).LostKobrakai
I mean I can see that having everything as proper macros might be desirable, but all of them work the same. You need to bind the value to a variable before you can use the variable in a guard clause.
I’d consider that inconsistent with how the rest of the language works and I’d consider how it works with function headers here as well. Where is the
valuedefined that’s been used withnon_nil/1? Given that guard clauses in function heads don’t work well with arbitrary macros I also don’t see it as an outlyer that pattern matches infor,case,withwould work vastly different.alexiss
Just for fun..
After looking into sources I’ve found that
withuses:elixir_utils.extract_guardsto process guards. So potentially it’s possible to implement something like “inline guards” (may be and for functions too) if rewrite this code:with
thats gives somethink like this:
Don’t take this comment seriously )
Anyway, @OvermindDL1 - thanks for a quick reply
OvermindDL1
It’s defined via the
valuein that expression, which would be more clear perhaps without the parenthesis:And yet it is not? You can define a value just fine via a macro and indeed that is done in many cases in many libraries. As well as you didn’t have to specify a value to bind to, with my above macro you could just as well do:
That is why I often have default values in my macro’s for such bindings, so either method works just fine.
They work just fine if the macro is written to only return guard-safe calls:
Actually it is on purpose that they don’t support macro’s in those positions, so sadly it won’t be fixed. However, you can of course make your own library to replace those functions with ones better suited for your purpose. ^.^
I’m actually a fan of having a macro that operates on an entire
defmoduledeclaration to mutate it en-masse to ‘fix-up’ inconsistencies like that (among other things).