viniciusalonso
Elixir new type system helpful gist
Hi everyone!
I’m working in a gist with some concepts around elixir new type system. This is a WIP and I’ll add more content on the next days.
Do you have any suggestion? Let me know.
Most Liked
trentjones21
I’m assuming there is a good reason, but do you know the purpose of the guard in this example?
$ %{age: integer(), ...} -> integer()
def get_age(person) when is_integer(person.age), do: person.age
Is it not redundant to include the when is_integer guard if the compiler checks types?
Edit: After thinking about it more, I think it’s because the types will let the compiler check types, but the types won’t have any effect on pattern matching / function overloading (not sure if it’s called that in the Elixir world). Is that right?
viniciusalonso
Exactly!
The example below could illustrate it.
$ (integer() -> integer()) and (boolean() -> boolean())
def negate(x) when is_integer(x), do: -x
def negate(x) when is_boolean(x), do: not x
rvirding
Also there is nothing from stopping the function to be called with completely wrong argument types. It mightn’t happen from within the type section but there is no safety from the “outside”.
Last Post!
RudManusachi
In the example for “Set-Theoric Types (unions, intersections and negations)”
# Can you find a problem here?
$ (integer() or boolean()) -> (integer() or boolean())
def negate(x) when is_integer(x), do: -x
def negate(x) when is_boolean(x), do: not x
$ (integer() -> integer()) and (boolean() -> boolean())
def negate(x) when is_integer(x), do: -x
def negate(x) when is_boolean(x), do: not x
the tricky one would be
# Can you find a problem here?
$ (integer() -> integer()) or (boolean() -> boolean())
def negate(x) when is_integer(x), do: -x
def negate(x) when is_boolean(x), do: not x
$ (integer() -> integer()) and (boolean() -> boolean())
def negate(x) when is_integer(x), do: -x
def negate(x) when is_boolean(x), do: not x
Popular in Discussions
Other popular topics
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










