You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,6 +94,44 @@ but expected a type that implements the Enumerable protocol, it must be one of:
94
94
) or fun() or list(term()) or non_struct_map()
95
95
```
96
96
97
+
### Type checking and inference of anonymous functions
98
+
99
+
Elixir v1.19 can now type infer and type check anonymous functions. Here is a trivial example:
100
+
101
+
```elixir
102
+
defmoduleExampledo
103
+
defrundo
104
+
fun =fn %{} ->:mapend
105
+
fun.("hello")
106
+
end
107
+
end
108
+
```
109
+
110
+
The example above has an obvious typing violation, as the anonymous function expects a map but a string is given. With Elixir v1.19, the following warning is now printed:
111
+
112
+
```
113
+
warning: incompatible types given on function application:
114
+
115
+
fun.("hello")
116
+
117
+
given types:
118
+
119
+
binary()
120
+
121
+
but function has type:
122
+
123
+
(dynamic(map()) -> :map)
124
+
125
+
typing violation found at:
126
+
│
127
+
6 │ fun.("hello")
128
+
│ ~
129
+
│
130
+
└─ mod.exs:6:8: Example.run/0
131
+
```
132
+
133
+
Function captures, such as `&String.to_integer/1`, will also propagate the type as of Elixir v1.19, arising more opportunity for Elixir's type system to catch bugs in our programs.
134
+
97
135
## Faster compile times in large projects
98
136
99
137
This release includes two compiler improvements that can lead up to 4x faster builds in large codebases.
Copy file name to clipboardExpand all lines: lib/elixir/pages/references/gradual-set-theoretic-types.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,9 +101,9 @@ This function also has the type `(boolean() -> boolean())`, because it receives
101
101
102
102
At this point, you may ask, why not a union? As a real-world example, take a t-shirt with green and yellow stripes. We can say the t-shirt belongs to the set of "t-shirts with green color". We can also say the t-shirt belongs to the set of "t-shirts with yellow color". Let's see the difference between unions and intersections:
103
103
104
-
*`(t_shirts_with_green() or t_shirts_with_yellow())` - contains t-shirts with either green or yellow, such as green, green and red, green and yellow, yellow, yellow and red, etc.
104
+
*`(t_shirts_with_green() or t_shirts_with_yellow())` - contains t-shirts with either green or yellow, such as green, green and red, green and yellow, but also only yellow, yellow and red, etc.
105
105
106
-
*`(t_shirts_with_green() and t_shirts_with_yellow())` - contains t-shirts with both green and yellow (and also other colors)
106
+
*`(t_shirts_with_green() and t_shirts_with_yellow())` - contains t-shirts with both green and yellow (and maybe other colors)
107
107
108
108
Since the t-shirt has both colors, we say it belongs to the intersection of both sets. The same way that a function that goes from `(integer() -> integer())` and `(boolean() -> boolean())` is also an intersection. In practice, it does not make sense to define the union of two functions in Elixir, so the compiler will always point to the right direction.
0 commit comments