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
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Hey folks,
I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Hi everyone,
I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










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..