kokolegorille
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 erlangelist/examples/blackjack/lib/blackjack/deck.ex at dc7cd1d2c77e52fa0a3a90f269c0f4ca8cca908b · sasa1977/erlangelist · GitHub 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
Trending in Questions
Other Trending Topics
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #elixirconf-us
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
michalmuskala
Dialyzer will report errors only if they will always happen, not if they might happen. This is what the slogan “Dialyzer is never wrong” means in practice.
It might be that you only call the function with values that cause a legal return - it doesn’t know, so it assumes that’s the case until it can prove otherwise.
kokolegorille
@michalmuskala Thanks for your reply.
So I assume it is why dyalizer cannot prove you are right, but can only prove you are wrong
peerreynders
Dialyzer assumes you are right until it can prove you wrong.
See Learn You Some Erlang: Success Typing.
kokolegorille
@peerreynders Thank You for your reply. The link is really nice.
peerreynders
.
Alternately
kokolegorille
@peerreynders That is perfect, Thanks!
It took me a little time to read the link on Learn You some Erlang, but it is very deep and informative. I will put this book on top of my reading list.
The key point is to have those private functions that can be tested individually.
alfert
This is a nice example. It highlights that it is (at least sometimes) very helpful to have proper functions to produce rather simple values. It is again one place where the data construction is concentrated making refactoring much easier, showing the power of abstractions.
However, using the ubiquitous Erlangish/Elixirish version of the Maybe-Type
{:ok, any} | {:error, any}consequently in that style would require to do the pattern matching on the receiving side in a similar functional style (i.e. properly abstracted) to be able to change the internal representation. Has anybody an idea how to do this elegantly (and checkable)? What comes into my mind is to use pipelining in a monadic fashion, but this moves the problem only to the local functions:Any suggestions?
peerreynders
This post mentions exceptional (Exceptional: Freedom from {:error}s: An error handling library (and philosophy) for Elixir) for smoother handling of tagged tuples - though I haven’t myself looked into it yet.
OvermindDL1
Yeah I am quite the proponant of
exceptional. It is such a great mix of monadic handling but in an entirely elixir and backwards-compatible way.alfert
Thanks for the hint.
Exceptionallooks like a promising approach, even with the new operators~>and>>>, where I am always be a little reluctant. They make the code concise and more difficult to read at the same time, depending on how much these operators are in the main stream and properly understood by most of us.