raytracer
What do you think about pre and post condition checks in Elixir?
I’ve just started learning Elixir and can see how guards are similar to pre-condition checks. Is there anything that could be used for post-condition checks?
Last night I realised the pipe operator could be used (abused?) to help implement post-condition checks and wrote this marcro:
defmacro ensure input, pattern1, pattern2 do
quote do
case unquote(input) do
unquote(pattern1) -> unquote(input)
unquote(pattern2) -> unquote(input)
_ -> raise PostConditionError
end
end
end
It would be used like:
def is_food(input) do
case input do
"Cheese" -> true
"Beer" -> {:error, "Item is not food"}
"Phone" -> false
end
|> ensure(true, {:error, _reason})
end
A is_food("Phone") call would result in a PostConditionError being raised.
Thoughts?
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
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
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
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
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
AcmeScript — Writing JS hooks as if I were still using Elixir
I’ve been having fun building a little something over the last few days: Ac...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You might be able to create a macro
def/3, which allows for a syntax likechristhekeele
Worth noting this is already valid syntax with very different implications: it creates an implicit
tryblock around the function and ensures your code runs after it whether or not it raised; and does not allow accessing the result of the function (since there may be none since it could have raised).Playing around with a couple of observations:
What you propose is still actually
def/2: the macro receives a call and a keyword list of blocks, so in our example the macro would receive:Semantically what we want is actually closer to the
elseblock in atrythat matches on the result iff no error was raised, allowing for post-checks and result transformation:You can’t make up your own block keywords, the parser only allows usage of the existing secondary-block terms after a
do:catch,rescue,afterandelseThe existing implementation of
defwith an implicittryonly usescatch,rescue, andafterConclusion: We could make a custom
def/2macro that employs anelseblock and transforms the provided implementation into something traditionally allowed inKernel.def/2, andelseis already the only syntactically valid unused block keyword inKernel.def/2.Cons:
def(head, do: body, else: postcheck)does not indicate intent nearly as well asdef(head, do: body, after: postcheck). But such a macro would definitely be possible!Alternatively:
Since block arguments must come last, the only form of
def/3the parser would recognize would be:This isn’t that great though:
Interestingly we could employ a fictional
ensure/1function that the parser doesn’t choke on, to make the intent more legible:Cons: certain complicated expressions could make the parser decide it no longer likes this construct, it’s arguably even hackier though it reads well..