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.

5 Likes

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?

2 Likes

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
2 Likes

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”.

2 Likes

Are there plans for any sort of “Typings” equivalent, so you can get compiler error type checking when calling a dependency?

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