|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Elixir v1.17 released: set-theoretic types in patterns, calendar durations, and Erlang/OTP 27 support" |
| 4 | +author: Andrea Leopardi |
| 5 | +category: Releases |
| 6 | +excerpt: "Elixir v1.17 released: set-theoretic types in patterns, calendar durations, and Erlang/OTP 27 support" |
| 7 | +--- |
| 8 | + |
| 9 | +Elixir v1.17 has just been released. 🎉 |
| 10 | + |
| 11 | +This release introduces set-theoretic types into a handful of language constructs. While there are still [many steps ahead of us](https://elixir-lang.org/blog/2023/06/22/type-system-updates-research-dev/), this important milestone already brings benefits to developers in the form of new warnings for common mistakes. This new version also adds support for [Erlang/OTP 27](https://www.erlang.org/downloads/27), the latest and greatest Erlang release. You'll also find a new calendar-related data type (`Duration`) and a `Date.shift/2` function. |
| 12 | + |
| 13 | +Let's dive in. |
| 14 | + |
| 15 | +## Warnings from gradual set-theoretic types |
| 16 | + |
| 17 | +This release introduces gradual set-theoretic types to infer types from patterns and use them to type check programs, enabling the Elixir compiler to find faults and bugs in codebases without requiring changes to existing software. The underlying principles, theory, and roadmap of our work have been outlined in ["The Design Principles of the Elixir Type System" by Giuseppe Castagna, Guillaume Duboc, José Valim](https://arxiv.org/abs/2306.06391). |
| 18 | + |
| 19 | +At the moment, Elixir developers will interact with set-theoretic types only through **warnings** found by the type system. The current implementation models all data types in the language: |
| 20 | + |
| 21 | + * `binary()`, `integer()`, `float()`, `pid()`, `port()`, `reference()` - these |
| 22 | + types are indivisible. This means both `1` and `13` get the same `integer()` |
| 23 | + type. |
| 24 | + |
| 25 | + * `atom()` - it represents all atoms and it is divisible. For instance, the |
| 26 | + atom `:foo` and `:hello_world` are also valid (distinct) types. |
| 27 | + |
| 28 | + * `map()` and structs - maps can be "closed" or "open". Closed maps only allow |
| 29 | + the specified keys, such as `%{key: atom(), value: integer()}`. Open maps |
| 30 | + support any other keys in addition to the ones listed and their definition |
| 31 | + starts with `...`, such as `%{..., key: atom(), value: integer()}`. Structs |
| 32 | + are closed maps with the `__struct__` key. |
| 33 | + |
| 34 | + * `tuple()`, `list()`, and `function()` - currently they are modelled as |
| 35 | + indivisible types. The next Elixir versions will also introduce fine-grained |
| 36 | + support to them. |
| 37 | + |
| 38 | +We focused on *atoms* and *maps* on this initial release as they are respectively the simplest and the most complex types representations, so we can stress the performance of the type system and quality of error messages. Modelling these types will also provide the most immediate benefits to Elixir developers. Assuming there is a variable named `user`, holding a `%User{}` struct with a `address` field, Elixir v1.17 will emit the following warnings at compile-time: |
| 39 | + |
| 40 | + * Pattern matching against a map or a struct that does not have the given key, |
| 41 | + such as `%{adress: ...} = user` (notice `address` vs `adress`). |
| 42 | + |
| 43 | + * Accessing a key on a map or a struct that does not have the given key, such |
| 44 | + as `user.adress`. |
| 45 | + |
| 46 | + * Updating a struct or a map that does not define the given key, such as |
| 47 | + `%{user | adress: ...}`. |
| 48 | + |
| 49 | + * Invoking a function on non-modules, such as `user.address()`. |
| 50 | + |
| 51 | + * Capturing a function on non-modules, such as `&user.address/0`. |
| 52 | + |
| 53 | + * Attempting to call an anonymous function without an actual function, such as |
| 54 | + `user.()`. |
| 55 | + |
| 56 | + * Performing structural comparisons between structs, such as `my_date < |
| 57 | + ~D[2010-04-17]`. |
| 58 | + |
| 59 | + * Performing structural comparisons between non-overlapping types, such as |
| 60 | + `integer >= string`. |
| 61 | + |
| 62 | + * Building and pattern matching on binaries without the relevant specifiers, |
| 63 | + such as `<<name>>` (this warns because by default it expects an integer, it |
| 64 | + should have been `<<name::binary>>` instead). |
| 65 | + |
| 66 | + * Attempting to rescue an undefined exception or a struct that is not an |
| 67 | + exception. |
| 68 | + |
| 69 | + * Accessing a field that is not defined in a rescued exception. |
| 70 | + |
| 71 | +Here's an example of how the warning for accessing a misspelled field of a |
| 72 | +struct looks like: |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +Another example, this time it's a warning for structural comparison across two |
| 77 | +`Date` structs: |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +These warnings also work natively in text editors, as they are standard Elixir |
| 82 | +compiler warnings: |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +These new warnings will help Elixir developers find bugs earlier and give more |
| 87 | +confidence when refactoring code, especially around maps and structs. While |
| 88 | +Elixir already emitted some of these warnings in the past, those were discovered |
| 89 | +using syntax analysis. The new warnings are more reliable, precise, and with |
| 90 | +better error messages. Keep in mind, however, that the Elixir typechecker only |
| 91 | +infers types from patterns within the same function at the moment. Analysis from |
| 92 | +guards and across function boundaries will be added in future releases. For more |
| 93 | +details, see our new [reference document on gradual set-theoretic |
| 94 | +types](https://hexdocs.pm/elixir/main/gradual-set-theoretic-types.html). |
| 95 | + |
| 96 | +The type system was made possible thanks to a partnership between |
| 97 | +[CNRS](https://www.cnrs.fr/) and [Remote](https://remote.com/). The development |
| 98 | +work is currently sponsored by [Fresha](https://www.fresha.com/) |
| 99 | +([they are hiring!](https://www.fresha.com/careers/openings?department=engineering)), |
| 100 | +[Starfish*](https://starfish.team/), and [Dashbit](https://dashbit.co/). |
| 101 | + |
| 102 | +## Erlang/OTP support |
| 103 | + |
| 104 | +This release adds support for Erlang/OTP 27 and drops support for Erlang/OTP 24. |
| 105 | +We recommend Elixir developers to migrate to Erlang/OTP 26 or later, especially |
| 106 | +on Windows. Support for WERL (a graphical user interface for the Erlang terminal |
| 107 | +on Windows) will be removed in Elixir v1.18. |
| 108 | + |
| 109 | +You can read more about Erlang/OTP 27 in [their release |
| 110 | +announcement](https://www.erlang.org/downloads/27). The bits that are |
| 111 | +particularly interesting for Elixir developers are the addition of a [`json` |
| 112 | +module](https://erlang.org/documentation/doc-15.0-rc3/lib/stdlib-6.0/doc/html/json.html) |
| 113 | +and process labels (`proc_lib:set_label/1`). The latter will also be available |
| 114 | +in this Elixir release as `Process.set_label/1`. |
| 115 | + |
| 116 | +## New `Duration` data type and shifting functions |
| 117 | + |
| 118 | +This Elixir version introduces the `Duration` data type and APIs to shift dates, |
| 119 | +times, and date times by a given duration, considering different calendars and |
| 120 | +time zones. |
| 121 | + |
| 122 | +```elixir |
| 123 | +iex> Date.shift(~D[2016-01-31], month: 2) |
| 124 | +~D[2016-03-31] |
| 125 | +``` |
| 126 | + |
| 127 | +We chose the name *"shift"* for this operation (instead of "add") since working |
| 128 | +with durations does not obey properties such as **associativity**. For instance, |
| 129 | +adding one month and then one month does not give the same result as adding two |
| 130 | +months: |
| 131 | + |
| 132 | +```elixir |
| 133 | +iex> ~D[2016-01-31] |> Date.shift(month: 1) |> Date.shift(month: 1) |
| 134 | +~D[2016-03-29] |
| 135 | +``` |
| 136 | + |
| 137 | +Still, durations are essential for building intervals, recurring events, and |
| 138 | +modelling scheduling complexities found in the world around us. For `DateTime`s, |
| 139 | +Elixir will correctly deal with time zone changes (such as Daylight Saving |
| 140 | +Time). However, provisions are also available in case you want to surface |
| 141 | +conflicts, such as shifting to a wall clock that does not exist, because the |
| 142 | +clock has been moved forward by one hour. See `DateTime.shift/2` for examples. |
| 143 | + |
| 144 | +Finally, we added a new `Kernel.to_timeout/1` function, which helps developers |
| 145 | +normalize durations and integers to a timeout used by many APIs—like `Process`, |
| 146 | +`GenServer`, and more. For example, to send a message after one hour, you can |
| 147 | +now write: |
| 148 | + |
| 149 | +```elixir |
| 150 | +Process.send_after(pid, :wake_up, to_timeout(hour: 1)) |
| 151 | +``` |
| 152 | + |
| 153 | +## Learn more |
| 154 | + |
| 155 | +Here are other notable changes in this release: |
| 156 | + |
| 157 | + * There are new `Keyword.intersect/2,3` functions to mirror the equivalent in |
| 158 | + the `Map` module. |
| 159 | + |
| 160 | + * A new Mix profiler was added, `mix profile.tprof`, which lets you use the |
| 161 | + new [tprof](https://www.erlang.org/doc/apps/tools/tprof.html) |
| 162 | + profiler released with Erlang/OTP 27. This profiler leads to the |
| 163 | + soft-deprecation of `mix profile.cprof` and `mix profile.eprof`. |
| 164 | + |
| 165 | + * We added `Kernel.is_non_struct_map/1`, a new guard to help with the common |
| 166 | + pitfall of matching on `%{}`, which also successfully matches structs (as |
| 167 | + they are maps underneath). |
| 168 | + |
| 169 | + * Elixir's Logger now formats |
| 170 | + [`gen_statem`](https://www.erlang.org/doc/apps/stdlib/gen_statem.html) |
| 171 | + reports and includes Erlang/OTP 27 *process labels* in logger events. |
| 172 | + |
| 173 | +For a complete list of all changes, see the |
| 174 | +[full release notes](https://github.com/elixir-lang/elixir/releases/tag/v1.17.0). |
| 175 | + |
| 176 | +Check [the Install section](/install.html) to get Elixir installed and |
| 177 | +read our [Getting Started guide](https://hexdocs.pm/elixir/introduction.html) |
| 178 | +to learn more. |
| 179 | + |
| 180 | +Happy learning! |
0 commit comments