Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/elixir/lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ defprotocol Inspect do

* `:except` - remove the given fields when inspecting.

* `:optional` - (since v1.14.0) do not include a field if it
matches its default value. This can be used to simplify the
struct representation at the cost of hiding information.
* `:optional` - (since v1.14.0) a list of fields that should not be
included when they match their default value. This can be used to
simplify the struct representation at the cost of hiding
information.

Whenever `:only` or `:except` are used to restrict fields,
the struct will be printed using the `#User<...>` notation,
Expand All @@ -82,7 +83,15 @@ defprotocol Inspect do
#=> #User<id: 1, name: "Jane", ...>

If you use only the `:optional` option, the struct will still be
printed as `%User{...}`.
printed as a valid struct.

defmodule Point do
@derive {Inspect, optional: [:z]}
defstruct [x: 0, y: 0, z: 0]
end

inspect(%Point{x: 1})
%Point{x: 1, y: 0}

## Custom implementation

Expand Down
Loading