Skip to content

Commit 068015e

Browse files
authored
Add more docs about :optional in Inspect (#14399)
1 parent 25f063e commit 068015e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/elixir/lib/inspect.ex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ defprotocol Inspect do
6464
6565
* `:except` - remove the given fields when inspecting.
6666
67-
* `:optional` - (since v1.14.0) do not include a field if it
68-
matches its default value. This can be used to simplify the
69-
struct representation at the cost of hiding information.
67+
* `:optional` - (since v1.14.0) a list of fields that should not be
68+
included when they match their default value. This can be used to
69+
simplify the struct representation at the cost of hiding
70+
information.
7071
7172
Whenever `:only` or `:except` are used to restrict fields,
7273
the struct will be printed using the `#User<...>` notation,
@@ -82,7 +83,15 @@ defprotocol Inspect do
8283
#=> #User<id: 1, name: "Jane", ...>
8384
8485
If you use only the `:optional` option, the struct will still be
85-
printed as `%User{...}`.
86+
printed as a valid struct.
87+
88+
defmodule Point do
89+
@derive {Inspect, optional: [:z]}
90+
defstruct [x: 0, y: 0, z: 0]
91+
end
92+
93+
inspect(%Point{x: 1})
94+
%Point{x: 1, y: 0}
8695
8796
## Custom implementation
8897

0 commit comments

Comments
 (0)