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 Responses

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

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement