Slesa

Slesa

Functional Domain Programming for Elixir?

I read the book “Domain Modelllng Made Functional” several times. And it was absolute clear, that I want to have some types sich as

“Unchecked Order” – read → “Verified Order” – send → “Dispatched Order”

to be sure, that I always deal with the right type of order, for what reason soever. An unread order cannot be send to database for example.

But as Elixir has not really a type system (yet) - is this the right way to go, or how would I deal with this? When I get an order, I need to verify waiter rights. I have to check if I have to ask for length, price, weight, article text, constrainted articles… would I reflect these rules by types? Or would I simply trust on the pipe chain?

Most Liked

stefanluptak

stefanluptak

One problem I find with this approach is refactoring. Something like “find all occurences of this map shape” and rename a key there. Or make sure certain key is only of type integer etc.

katafrakt

katafrakt

That is a bit of my problem with many resources about functional programming - they cover static typing instead, which makes it not particularly well transferable to Elixir (or Clojure, or some others). In this case use different structs, as @RudManusachi mentioned, but in my experience this will quickly start to feel weird and overengineered (at least that’s my experience, but maybe I did that wrong and someone could share a success story of this approach in Elixir).

IMO Elixir needs to find its own way to do functional modeling, DDD etc, because copying from Scala or F# won’t work. I wrote something about this few years ago, but this is just scratching the surface (and not necessarily the only direction).

DaAnalyst

DaAnalyst

Maybe like this?

  defmodule Order do
    @type t( status) ::
      %__MODULE__{
        id: Ecto.UUID.t(),
        status: status
      }

    defstruct [ :id, :status]
  end

  @spec send_order( Order.t( :verified)) :: :ok | { :error, :unverified_order}
  def send_order( order)

  def send_order( %{ status: :verified}) do
    :ok
  end

  def send_order( %{ status: _}) do
    { :error, :unverified_order}
  end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement