tomekowal
I am reading “Domain modelling made functional” and many ideas resonate with me. The examples are in F♯ but some ideas are general and transferrable to all languages.
One idea is that we should write types like UnverifiedEmail and VerifiedEmail and then
@type unvalidated_email() :: String.t()
@type validated_email() :: String.t()
@spec validate_email(unvalidated_email()) :: validated_email()
def validate_email(u), do: u
@spec an_email() :: unvalidated_email()
def an_email(), do: "example@gmail.com"
@spec send_message(validated_email()) :: :ok
def send_message(_e), do: :ok
@spec run() :: :ok
def run() do
send_message(an_email())
:ok
end
In F♯ code like that would fail to compile because send_message tries to use unverified email. Dialyzer success typing passes so I can’t enforce it with Dialyzer.
Another approach would be to use an %UnverifiedEmail{} struct and %VerifiedEmail{} structs all over the code.
That would require creating a lot of small (one field) structs. My questions are: have anyone tried modelling the domain with multiple structs? How much using a lot of structs affected compilation time?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
Probably too naive an approach for your purpose - but here it is.
hlalvesbr
Erlang and Elixir are more on the dynamic typed languages field. If you want strong guarantees at compile time, you should try Haskell, OCaml or F#.
tomekowal
Thanks. I need OTP so I won’t move to other language. I am just researching tools that might improve design in the project. I’ll check out Witchcraft GitHub - witchcrafters/witchcraft: Monads and other dark magic for Elixir · GitHub It is not really idiomatic so I wanted to check if I can “simulate” type safety with something simpler.
Do you know any statically typed languages for BEAM? I know about GitHub - wende/elchemy: Write Elixir code using statically-typed Elm-like syntax (compatible with Elm tooling) · GitHub GitHub - alpaca-lang/alpaca: Functional programming inspired by ML for the Erlang VM · GitHub
stefanluptak
zkessin
The problem is that both types are defined as String.t(), what you want to do is define the types in some way that they can not mix. such as
{:validated_email, String.t()}and{:unvalidated_email, String.t()}then dialyzer (and your pattern matching) can validate things.In general, in terms of types, a String is a String. so using an atom in a tuple or other data structure allows dialyzer to catch this.
This video goes over this in more details
al2o3cr
I believe the “standard Elixir” way would be using tuples like @zkessin mentioned above; I read the Elixir form:
as mostly-equivalent to the Elm/Haskell form:
Code can pattern-match values out of a tuple in function heads /
caseblocks.and
withblocks can work likedo-notation:imetallica
Honestly, you cannot validate that on compile time, but you can enforce that on runtime via pattern matching. I would suggest something like this, although I feel it’s a bit of overkill:
Moxide
Hello, you are not alone to “resonate” with the ideas presented in this great book
When I tried to reproduce in Elixir some on the examples given, I used the “all is struct” approach and the typed_struct library.
Then played with VSCode and dialyzer (using the dialyzer
underspecsandoverspecsoptions).Here is the code (just a POC ! ;-)) :
Depending of the type of error you introduce (missing return type in
@specfor instance) and on the Dialyzer options, it will be too verbose or too silent.tomekowal
So, there is at least a couple of different methods.
Using tagged tuples, structs, macros with structs alone, macros generating structs and types like in
typed_structoralgae.Building on that there are a couple of libraries for handling flows with errors ok_jose, exceptional, witchcraft (which has also more functors, monands, monodis and so on).
I wish there was a standard way of doing this stuff either baked into the language or a library that people agree to use (like it was with Timex).
Thanks for your input!
woohaaha
It has been some time since you’ve asked this question. Have you settled on a pattern you enjoy using? If so, mind sharing a contrived elixir example? Thank you