Skip to content

Commit aff4fdb

Browse files
committed
Update CHANGELOG
1 parent 793c114 commit aff4fdb

File tree

2 files changed

+172
-6
lines changed

2 files changed

+172
-6
lines changed

CHANGELOG.md

Lines changed: 170 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,117 @@
11
# Changelog for Elixir v1.18
22

3-
TODO.
3+
Elixir v1.18 is an impressive release with improvements across the two main efforts happening within the Elixir ecosystem right now: set-theoretic types and language servers. It also comes with built-in JSON support and adds new capabilities to its unit testing library. Here is a quick break down.
44

55
## Type system improvements
66

7+
The most exciting change in Elixir v1.18 is typing checking of function calls, alongside gradual inference of patterns and return types. To understand how this will impact your programs, consider the following code:
8+
9+
```elixir
10+
defmodule User do
11+
defstruct [:age, :car_choice]
12+
13+
def drive(%User{age: age, car_choice: car}, cars_choices) when age >= 18 do
14+
if car in car_choices do
15+
{:ok, car}
16+
else
17+
{:error, :no_choice}
18+
end
19+
end
20+
21+
def drive(%User{}, _car_choices) do
22+
{:error, :not_allowed}
23+
end
24+
end
25+
```
26+
27+
Elixir's type system will infer the drive function expects a `%User{}` struct as input and returns either `{:ok, dynamic()}` or `{:error, :no_choice}` or `{:error, :not_allowed}`.
28+
29+
Therefore, the following code should emit a violation, due to an invalid argument:
30+
31+
```elixir
32+
User.drive({:ok, %User{}}, car_choices)
33+
```
34+
35+
Here is the warning:
36+
37+
```
38+
warning: incompatible types given to User.drive/2:
39+
40+
User.drive({:ok, %User{age: nil, car_choice: nil}}, car_choices)
41+
42+
given types:
43+
44+
{:ok, %User{age: nil, car_choice: nil}}, empty_list()
45+
46+
but expected one of:
47+
48+
dynamic(%User{age: term(), car_choice: term()}), dynamic()
49+
50+
where "car_choices" was given the type:
51+
52+
# type: empty_list()
53+
# from: lib/foo.ex:21:17
54+
car_choices = []
55+
56+
typing violation found at:
57+
58+
22 │ User.drive({:ok, %User{}}, car_choices)
59+
│ ~
60+
61+
└─ lib/foo.ex:22:10: Example.run/0
62+
```
63+
64+
> The mismatched arguments are shown in red, if your terminal supports ANSI coloring.
65+
66+
And the next snippet will warn because the `:error` clause will never match, as that's not a valid return type of the `User.drive/2` call:
67+
68+
```elixir
69+
case User.drive(user, car_choices) do
70+
{:ok, car} -> car
71+
:error -> Logger.error("User cannot drive")
72+
end
73+
```
74+
75+
And here is the warning:
76+
77+
```
78+
warning: the following clause will never match:
79+
80+
:error
81+
82+
because it attempts to match on the result of:
83+
84+
User.drive(user, car_choices)
85+
86+
which has type:
87+
88+
dynamic({:ok, term()} or {:error, :no_choice} or {:error, :not_allowed})
89+
90+
typing violation found at:
91+
92+
26 │ :error -> Logger.error("User cannot drive")
93+
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
95+
└─ lib/foo.ex:26: Example.run/0
96+
```
97+
98+
For more details on typing inference and the trade-offs made by the Elixir team, [see our official documentation](https://hexdocs.pm/elixir/1.18/gradual-set-theoretic-types.html#type-inference).
99+
100+
There are many other improvements to the type system, which we will go in detail within the official release. Meanwhile, here is a list summary of the overall improvements done to the type system:
101+
7102
* Type inference of patterns (typing inference of guards will be part of an upcoming release)
8103

9104
* Type checking of all language constructs, including local and remote calls, except `for`, `with`, and closures
10105

11-
* Type checking of all `Kernel` and conversion functions inlined by the compiler
106+
* Type checking of all functions inlined by the compiler found in `Kernel`
107+
108+
* Type checking of all conversion functions inlined by the compiler
12109

13110
* [Support for tuples and lists as composite types](https://elixir-lang.org/blog/2024/08/28/typing-lists-and-tuples/) as well as type checking of their basic operations
14111

15112
* Detection of clauses and patterns that will never match from `case`, `cond`, and `=`
16113

17-
* Detection of unused clauses from private functions
114+
* Detection of unused clauses in private functions
18115

19116
## ExUnit improvements
20117

@@ -43,11 +140,68 @@ With features like async tests, suite partitioning, and now grouping, Elixir dev
43140

44141
## `mix format --migrate`
45142

46-
TODO.
143+
The `mix format` command now supports an explicit `--migrate` flag, which will convert constructs that have been deprecated in Elixir to their latest version. Because this flag rewrites the AST, it is not guaranteed the migrated format will always be valid when used in combination with macros that also perform AST rewriting.
144+
145+
As of this release, the following migrations are executed:
146+
147+
* Normalize parens in bitstring modifiers - it removes unnecessary parentheses in known bitstring modifiers, for example `<<foo::binary()>>` becomes `<<foo::binary>>`, or adds parentheses for custom modifiers, where `<<foo::custom_type>>` becomes `<<foo::custom_type()>>`.
148+
149+
* Charlists as sigils - formats charlists as `~c` sigils, for example `'foo'` becomes `~c"foo"`.
150+
151+
* `unless` as negated `if`s - rewrites `unless` expressions using `if` with a negated condition, for example `unless foo do` becomes `if !foo do`.
152+
153+
More migrations may be added in future releases.
154+
155+
## JSON support
156+
157+
This release includes official support for JSON encoding and decoding.
158+
159+
Both encoder and decoder fully conform to [RFC 8259](https://tools.ietf.org/html/rfc8259) and
160+
[ECMA 404](https://ecma-international.org/publications-and-standards/standards/ecma-404/)
161+
standards.
162+
163+
### Encoding
164+
165+
Encoding can be done via `JSON.encode!/1` and `JSON.encode_to_iodata!/1` functions.
166+
The default encoding rules are applied as follows:
167+
168+
| **Elixir** | **JSON** |
169+
|------------------------|----------|
170+
| `integer() \| float()` | Number |
171+
| `true \| false ` | Boolean |
172+
| `nil` | Null |
173+
| `binary()` | String |
174+
| `atom()` | String |
175+
| `list()` | Array |
176+
| `%{binary() => _}` | Object |
177+
| `%{atom() => _}` | Object |
178+
| `%{integer() => _}` | Object |
179+
180+
You may also implement the `JSON.Encoder` protocol for custom data structures.
181+
If you have a struct, you can derive the implementation of the `JSON.Encoder`
182+
by specifying which fields should be encoded to JSON:
183+
184+
```elixir
185+
@derive {JSON.Encoder, only: [....]}
186+
defstruct ...
187+
```
188+
189+
### Decoding
190+
191+
Decoding can be done via `JSON.decode/2` and `JSON.decode!/2` functions.
192+
The default decoding rules are applied as follows:
193+
194+
| **JSON** | **Elixir** |
195+
|----------|------------------------|
196+
| Number | `integer() \| float()` |
197+
| Boolean | `true \| false` |
198+
| Null | `nil` |
199+
| String | `binary()` |
200+
| Object | `%{binary() => _}` |
47201

48202
## Potential incompatibilities
49203

50-
This release no longer supports WERL (a graphical user interface on Windows used by Erlang 25 and earlier). For a better user experience on Windows terminals, use Erlang/OTP 26+.
204+
This release no longer supports WERL (a graphical user interface on Windows used by Erlang 25 and earlier). For a better user experience on Windows terminals, use Erlang/OTP 26+ (this is also the last Elixir release to support Erlang/OTP 25).
51205

52206
Furthermore, in order to support inference of patterns, Elixir will raise if it finds recursive variable definitions. This means patterns that never match, such as this one, will no longer compile:
53207

@@ -72,6 +226,7 @@ You may also prefer to write using guards:
72226
#### Elixir
73227

74228
* [CLI] Add experimental PowerShell scripts for `elixir`, `elixirc`, and `mix` on Windows. Those provide a safer entry point for running Elixir from other platforms
229+
* [Calendar] Add `Duration.to_string/1`
75230
* [Code] Support several migration options in `Code.format_string!/2`
76231
* [Code] Add parenthesis around `--` and `---` in `Code.format_string!/2` to make precedence clearer
77232
* [Code] Include more metadata in `Code.string_to_quoted/2` when `token_metadata: true` to help compute ranges from the AST
@@ -86,18 +241,23 @@ You may also prefer to write using guards:
86241
* [Kernel] Perform validation of root AST nodes in `unquote` and `unquote_splicing` to catch bugs earlier
87242
* [Kernel] Add source, behaviour, and record information to Docs chunk metadata
88243
* [Kernel] Support deterministic builds in tandem with Erlang by setting `ERL_COMPILER_OPTIONS=deterministic`. Keep in mind deterministic builds strip source and other compile time information, which may be relevant for programs
244+
* [Kernel] Allow aliases and imports to be enabled conditionally in module body
89245
* [List] Add `List.ends_with?/2`
90246
* [Macro] Improve `dbg` handling of `if/2`, `with/1` and of code blocks
91247
* [Macro] Add `Macro.struct_info!/2` to return struct information mirroring `mod.__info__(:struct)`
92248
* [Registry] Add `Registry.lock/3` for local locking
93249
* [PartitionSupervisor] Add `PartitionSupervisor.resize!/2` to resize the number of partitions in a supervisor (up to the limit it was started with)
94250
* [Process] Handle arbitrarily high integer values in `Process.sleep/1`
251+
* [Protocol] Add `@undefined_impl_description` to customize error message when an implementation is undefined
252+
* [Protocol] Add `__deriving__` as optional macro callback to `Protocol`, no longer requiring empty implementations
95253
* [String] Inspect special whitespace and zero-width characters using their Unicode representation
254+
* [String] Update Unicode to 16.0
96255

97256
#### ExUnit
98257

99258
* [ExUnit] Support parameterized tests on `ExUnit.Case`
100259
* [ExUnit] Support test groups: tests in the same group never run concurrently
260+
* [ExUnit.Case] Add `test_pid` as a tag
101261

102262
#### IEx
103263

@@ -109,6 +269,7 @@ You may also prefer to write using guards:
109269
* [mix compile] Ensure only a single operating system process can compile at a given time
110270
* [mix deps.get] Ensure only a single operating system process can fetch deps at a given time
111271
* [mix format] Add `mix format --migrate` to migrate from deprecated functionality
272+
* [mix format] Add new options and metadata to improve formatting applying by editors and other environments
112273
* [mix test] Taint failure manifest if requiring or compiling tests fail
113274
* [Mix.Project] Add a `:listeners` configuration to listen to compilation events from the current and other operating system processes
114275
* [Mix.Task.Compiler] Add API for fetching all persisted compiler diagnostics
@@ -122,13 +283,15 @@ You may also prefer to write using guards:
122283
* [Code.Formatter] Fix formatter adding extra escapes to quoted remote calls
123284
* [Code.Fragment] Properly handle keyword keys as their own entry
124285
* [Inspect.Algebra] Ensure `next_break_fits` respects `line_length`
286+
* [Kernel] Validate AST on `unquote` and `unquote_splicing` to provide better error reports instead of failing too late inside the compiler
125287
* [Module] Include module attribute line and name when tracing its aliases
126288
* [Stream] Do not halt streams twice in `Stream.transform/5`
127289
* [URI] Fix a bug when a schemaless URI is given to `URI.merge/2`
128290

129291
#### ExUnit
130292

131293
* [ExUnit.Assertions] Raise if guards are used in `assert/1` with `=`
294+
* [ExUnit.Assertions] Format inserted/deleted maps in list assertions
132295

133296
#### IEx
134297

@@ -140,6 +303,7 @@ You may also prefer to write using guards:
140303
* [mix deps.compile] Fix escaping issues when invoking `rebar3` in some cases
141304
* [mix escript] Fix escript layout and support storing `priv` directories
142305
* [mix release] Make `.app` files deterministic in releases
306+
* [Mix.Shell] Fix `Mix.Shell` on Windows when outputting non UTF-8 characters
143307

144308
### 3. Soft deprecations (no warnings emitted)
145309

@@ -149,6 +313,7 @@ You may also prefer to write using guards:
149313
* [Inspect.Algebra] `fold_doc/2` is deprecated in favor of `fold/2`
150314
* [Kernel] Deprecate `unless` in favor of `if`. Use `mix format --migrate` to automate the migration
151315
* [Macro] `Macro.struct!/2` is deprecated in favor of `Macro.struct_info!/2`
316+
* [Protocol] Defining the `__deriving__` macro inside the `Any` implementation is deprecated, derive it inside the protocol definition itself
152317

153318
### 4. Hard deprecations
154319

lib/elixir/lib/json.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,9 @@ defmodule JSON do
363363
364364
This function is most typically passed as second argument to
365365
`encode!/2` and `encode_to_iodata!/2`. The default implementation
366-
simply dispatches to `JSON.Encoder.encode/2`.
366+
is an optimized dispatch to the `JSON.Encoder` protocol.
367367
"""
368+
@spec protocol_encode(a, (a -> iodata())) :: iodata() when a: var
368369
def protocol_encode(value, encoder) when is_atom(value) do
369370
case value do
370371
nil -> "null"

0 commit comments

Comments
 (0)