Hello everyone,
I have been reading this book [Functional Programming] Domain Modeling Made Functional (Pragprog) (F#) and liked it a lot.
I am trying to mimic conditional type from F# to Elixir with the use of spec, type and dyalixir
defmodule Ddd.OrderTaking do
@type result :: :ok | :error
@spec create_unit_quantity(integer) :: result
def create_unit_quantity(qty) when is_integer(qty) do
cond do
qty < 0 -> :error
qty > 1_000 -> :error
true -> :ok
end
end
end
If I run mix dialyxir, everything is fine.
But if I change :ok to :okidoki, dyalizer does not detect any error.
If I change the 3 values to be invalid result, like :erroryo, and :okidoki then dialyxir detect the error, but not if one choice is valid.
I read documentation about spec, and type, but did not find any conditional example.
I saw this code in Sasa Juric Blackjack example here https://github.com/sasa1977/erlangelist/blob/dc7cd1d2c77e52fa0a3a90f269c0f4ca8cca908b/examples/blackjack/lib/blackjack/deck.ex that is using multiple choice
@type suit :: :spades | :hearts | :diamonds | :clubs
@type rank :: 2..10 | :jack | :queen | :king | :ace
But could not make it work with dialyxir. My expectation is to catch any invalid value.
Thanks for taking time
BTW I donât want to start a tabs vs spaces troll, but it is obvious spaces are more precise