tomekowal

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?

Showing Posts 1 to 10

peerreynders

peerreynders

Probably too naive an approach for your purpose - but here it is.

hlalvesbr

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

tomekowal OP

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

stefanluptak

zkessin

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.

@type validated_email() :: {:validated_email, String.t()}
@type unvalidated_email() :: {:unvalidated_email, String.t()}
@spec validate_email(unvalidated_email()) :: validated_email()
def validate_email({:unvalidated_email, u}), do: {:validated_email, u}

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

al2o3cr

I believe the “standard Elixir” way would be using tuples like @zkessin mentioned above; I read the Elixir form:

{:ok, value}

as mostly-equivalent to the Elm/Haskell form:

Ok value

Code can pattern-match values out of a tuple in function heads / case blocks.

case some_value do
  {:ok, result} ->
    # use result
  {:error, msg} ->
    IO.puts(msg)
end

and with blocks can work like do-notation:

with {:ok, result1} <- operation_1(),
     {:ok, result2} <- operation_2(result1) do
  # use result2
else
  {:error, :operation_1_failed} ->
    # etc
end
imetallica

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:

def an_email(), do: %UnvalidatedEmail{email: "foo@bar.com"}

@spec validate(UnvalidatedEmail.t) :: Email.t
def validate(%UnvalidatedEmail{email: email} do
  # Your validations here...
  %Email{email: email, valid: true}
end

@spec send_message(Email.t) :: :ok | {:error, term}
def send_message(%Email{valid: true} = email), do: :ok
def send_message(_), do: {:error, :invalid_email} 
Moxide

Moxide

Hello, you are not alone to “resonate” with the ideas presented in this great book :wink:
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 underspecs and overspecs options).

Here is the code (just a POC ! ;-)) :

defmodule OrderTakingTypes do
  defmodule OrderLines do
    use TypedStruct

    typedstruct do
      field(:lines, list(OrderLine.t()), default: [])
    end
  end

  defmodule OrderLine do
    use TypedStruct

    typedstruct do
      field(:product_code, String.t(), enforce: true)
      field(:quantity, number(), default: 0)
      field(:price, number(), default: 0)
    end
  end

  defmodule ShippingAddress do
    use TypedStruct

    typedstruct enforce: true do
      field(:town, String.t())
      field(:zip_code, String.t())
    end
  end

  defmodule Order do
    use TypedStruct

    typedstruct enforce: true do
      field(:shipping_address, ShippingAddress.t())
      field(:order_lines, list(OrderLine.t()))
    end
  end

  defmodule ValidatedOrder do
    use TypedStruct

    typedstruct enforce: true do
      field(:order, Order.t())
      field(:validation_date, DateTime.t())
    end
  end
end
defmodule TypeDemo do
  alias OrderTakingTypes.{OrderLines, OrderLine, ShippingAddress, Order, ValidatedOrder}

  def send() do
    %OrderLines{
      lines: [
        %OrderLine{price: 10, product_code: "SKU-0001", quantity: 5}
      ]
    }
    |> send_to(%ShippingAddress{zip_code: "69000", town: "Lyon"})
  end

  @spec send_to(OrderLines.t(), ShippingAddress.t()) :: :ok
  def send_to(%OrderLines{lines: order_lines}, %ShippingAddress{} = address) do
    %Order{
      shipping_address: address,
      order_lines: order_lines
    }
    |> validate_order()
    |> process_order()
  end

  @spec validate_order(Order.t()) :: ValidatedOrder.t() | ErrorResponse.t()
  def validate_order(%Order{order_lines: order_lines} = order) when length(order_lines) > 0 do
    %ValidatedOrder{
      order: order,
      validation_date: DateTime.utc_now()
    }
  end
  def validate_order(%Order{order_lines: order_lines}) when length(order_lines) == 0,
    do: %ErrorResponse{code: 500, message: "OrderLines cannot be empty"}
  def validate_order(_), do: %ErrorResponse{code: 500, message: "Unknown error"}

  @spec process_order(ValidatedOrder.t() | ErrorResponse.t()) :: :ok
  def process_order(%ValidatedOrder{
        order: %Order{shipping_address: %ShippingAddress{town: town, zip_code: zip_code}}
      }),
      do: IO.puts("Processed shipping to #{zip_code}, #{town}")
  def process_order(%ErrorResponse{code: code, message: message}),
    do: IO.puts("Error occured with code '#{code}' and message '#{message}'")
  def process_order(_), do: IO.puts("Weird error")
end

Depending of the type of error you introduce (missing return type in @spec for instance) and on the Dialyzer options, it will be too verbose or too silent.

tomekowal

tomekowal OP

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_struct or algae.
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

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

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews