viniciusalonso

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

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

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

rvirding

Creator of Erlang

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

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

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement