runexec
I was toying around to see if I could possibly get something similar to Clojure-style pre/post enforcement. This is what I came up with and would like your input. Thanks.
Example Usage
defmodule Example do
require Contract
def reverse_abc(x) do
# Contract pre processing
pre_must_be_abc = fn -> x == "abc" end
pre = [
pre_must_be_abc,
]
# Contract post processing
post_must_be_cba = fn x -> x == "cba" end
post_must_be_string = fn x -> is_binary x end
post = [
post_must_be_cba,
post_must_be_string,
]
# Contract
# The arguments here are `[x]` and do not represent the current value of `x`
reverse_abc_contract = Contract.new([x], [pre: pre, post: post], String.reverse(x))
# Encorce Contract
reverse_abc_contract.(x)
end
end
Basic Implementation
defmodule Contract do
defmodule EnforcePreError do
defexception message: "Argument(s) Failed Pre Processing Requrements."
end
defmacro enforce(:pre, list, expr) do
quote do
valid? =
Enum.map(unquote(list), fn f -> f.() end)
|> Enum.all?(&(&1 == true))
case valid? do
true -> unquote(expr)
_ -> raise EnforcePreError
end
end
end
defmodule EnforcePostError do
defexception message: "Return value Failed Post Processing Requrements."
end
defmacro enforce(:post, list, value) do
quote do
valid? =
Enum.map(unquote(list), fn f -> f.(unquote(value)) end)
|> Enum.all?(&(&1 == true))
case valid? do
true -> unquote(value)
_ -> raise EnforcePostError
end
end
end
defmodule EnforceNothingError do
defexception message: "No enforcements are defined."
end
defmacro enforce(:prepost, pre, post, expr) do
quote do
pre = unquote(pre) || []
post = unquote(post) || []
no_enforcements? = (pre == [] && post == [])
get_value = fn ->
unquote(__MODULE__).enforce(:pre, unquote(pre), unquote(expr))
end
cond do
no_enforcements? -> raise EnforceNothingError
pre && post -> unquote(__MODULE__).enforce(:post, unquote(post), get_value.())
(pre == []) && post -> unquote(__MODULE__).enforce(:post, unquote(post), unquote(expr))
(post == []) && pre -> get_value.()
end
end
end
defmacro new(args, opts, expr) do
quote do
fn (unquote_splicing(args)) ->
unquote(__MODULE__).enforce(
:prepost, unquote(opts[:pre]), unquote(opts[:post]), unquote(expr)
)
end
end
end
end
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
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 everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
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
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex









