Skip to content

Commit ebf8403

Browse files
author
José Valim
committed
Kernel.inspect/2 with Inspect.Opts[] is deprecated
In favor of `Inspect.Algebra.to_doc/2`. Closes #1965.
1 parent 1b3cdbc commit ebf8403

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
* Deprecations
1616
* [Kernel] `is_alive/0` is deprecated in favor of `Node.alive?`
17+
* [Kernel] `Kernel.inspect/2` with `Inspect.Opts[]` is deprecated in favor of `Inspect.Algebra.to_doc/2`
1718

1819
* Backwards incompatible changes
1920

lib/elixir/lib/inspect.ex

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ defprotocol Inspect do
1313
followed by the inspecting options, represented by the record
1414
`Inspect.Opts`.
1515
16-
Inspection is done using the functions available in
17-
`Inspect.Algebra` and by calling `Kernel.inspect/2` recursively
18-
passing the `Inspect.Opts` as an argument. When `Kernel.inspect/2`
19-
receives an `Inspect.Opts` record as the second argument, it returns
20-
the underlying algebra document instead of the formatted string.
16+
Inspection is done using the functions available in `Inspect.Algebra`.
2117
2218
## Examples
2319
@@ -29,15 +25,15 @@ defprotocol Inspect do
2925
import Inspect.Algebra
3026
3127
def inspect(dict, opts) do
32-
concat ["#HashSet<", Kernel.inspect(HashSet.to_list(dict), opts), ">"]
28+
concat ["#HashSet<", to_doc(HashSet.to_list(dict), opts), ">"]
3329
end
3430
end
3531
3632
The `concat` function comes from `Inspect.Algebra` and it
3733
concatenates algebra documents together. In the example above,
3834
it is concatenating the string `"HashSet<"` (all strings are
3935
valid algebra documents that keep their formatting when pretty
40-
printed), the document returned by `Kernel.inspect/2` and the
36+
printed), the document returned by `Inspect.Algebra.to_doc/2` and the
4137
other string `">"`.
4238
4339
Since regular strings are valid entities in an algebra document,
@@ -281,14 +277,14 @@ defimpl Inspect, for: List do
281277
keyword?(thing) && not opts.raw ->
282278
surround_many("[", thing, "]", opts.limit, &keyword(&1, opts))
283279
true ->
284-
surround_many("[", thing, "]", opts.limit, &Kernel.inspect(&1, opts))
280+
surround_many("[", thing, "]", opts.limit, &to_doc(&1, opts))
285281
end
286282
end
287283

288284
defp keyword({key, value}, opts) do
289285
concat(
290286
key_to_binary(key) <> ": ",
291-
Kernel.inspect(value, opts)
287+
to_doc(value, opts)
292288
)
293289
end
294290

@@ -329,7 +325,7 @@ defimpl Inspect, for: Tuple do
329325
def inspect(tuple, opts) do
330326
unless opts.raw do
331327
record_inspect(tuple, opts)
332-
end || surround_many("{", tuple_to_list(tuple), "}", opts.limit, &Kernel.inspect(&1, opts))
328+
end || surround_many("{", tuple_to_list(tuple), "}", opts.limit, &to_doc(&1, opts))
333329
end
334330

335331
## Helpers
@@ -339,7 +335,7 @@ defimpl Inspect, for: Tuple do
339335

340336
if is_atom(name) && (fields = record_fields(name)) && (length(fields) == size(record) - 1) do
341337
surround_record(name, fields, tail, opts)
342-
end || surround_many("{", [name|tail], "}", opts.limit, &Kernel.inspect(&1, opts))
338+
end || surround_many("{", [name|tail], "}", opts.limit, &to_doc(&1, opts))
343339
end
344340

345341
defp record_fields(name) do
@@ -373,7 +369,7 @@ defimpl Inspect, for: Tuple do
373369
end
374370

375371
defp keyword({ k, v }, opts) do
376-
concat(k <> ": ", Kernel.inspect(v, opts))
372+
concat(k <> ": ", to_doc(v, opts))
377373
end
378374
end
379375

@@ -422,11 +418,11 @@ defimpl Inspect, for: Regex do
422418
423419
"""
424420
def inspect(regex, opts) when size(regex) == 5 do
425-
concat ["%r", Kernel.inspect(Regex.source(regex), opts), Regex.opts(regex)]
421+
concat ["%r", to_doc(Regex.source(regex), opts), Regex.opts(regex)]
426422
end
427423

428424
def inspect(other, opts) do
429-
Kernel.inspect(other, opts.raw(true))
425+
to_doc(other, opts.raw(true))
430426
end
431427
end
432428

lib/elixir/lib/inspect/algebra.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ defmodule Inspect.Algebra do
100100
end
101101
end
102102

103+
@doc """
104+
Converts an Elixir structure to an algebra document
105+
according to the inspect protocol.
106+
"""
107+
@spec to_doc(any, Inspect.Opts.t) :: t
108+
def to_doc(arg, opts) when is_record(opts, Inspect.Opts) do
109+
case is_tuple(arg) do
110+
true ->
111+
case elem(opts, 1) do
112+
true -> Inspect.Tuple.inspect(arg, opts)
113+
false ->
114+
try do
115+
Inspect.inspect(arg, opts)
116+
catch
117+
_, _ -> Inspect.Tuple.inspect(arg, opts)
118+
end
119+
end
120+
false ->
121+
Inspect.inspect(arg, opts)
122+
end
123+
end
124+
103125
@doc """
104126
Returns `:doc_nil` which is a document entity used to represent
105127
nothingness. Takes no arguments.

lib/elixir/lib/kernel.ex

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,11 +1712,6 @@ defmodule Kernel do
17121712
The second argument is a keywords list with options to control
17131713
inspection.
17141714
1715-
The second argument may also be an instance of the `Inspect.Opts`
1716-
record and, in such cases, instead of returning a string, it
1717-
returns an algebra document which can be converted to a string
1718-
via `Inspect.Algebra`.
1719-
17201715
## Options
17211716
17221717
The following options are supported:
@@ -1754,35 +1749,21 @@ defmodule Kernel do
17541749
#=> #Function<...>
17551750
17561751
"""
1757-
@spec inspect(Inspect.t, Inspect.Opts.t) :: Inspect.Algebra.t
17581752
@spec inspect(Inspect.t, Keyword.t) :: String.t
17591753
def inspect(arg, opts // [])
17601754

1761-
def inspect(arg, opts) when is_tuple(opts) and tuple_size(opts) > 0 and
1762-
elem(opts, 0) == Inspect.Opts do
1763-
case is_tuple(arg) do
1764-
true ->
1765-
case elem(opts, 1) do
1766-
true -> Inspect.Tuple.inspect(arg, opts)
1767-
false ->
1768-
try do
1769-
Inspect.inspect(arg, opts)
1770-
catch
1771-
_, _ -> Inspect.Tuple.inspect(arg, opts)
1772-
end
1773-
end
1774-
false ->
1775-
Inspect.inspect(arg, opts)
1776-
end
1755+
def inspect(arg, opts) when is_tuple(opts) and tuple_size(opts) > 0 and elem(opts, 0) == Inspect.Opts do
1756+
IO.write "Kernel.inspect/2 with Inspect.Opts is deprecated, please use Inspect.Algebra.to_doc/2 instead\n#{Exception.format_stacktrace}"
1757+
Inspect.Algebra.to_doc(arg, opts)
17771758
end
17781759

17791760
def inspect(arg, opts) when is_list(opts) do
1780-
opts = Inspect.Opts.new(opts)
1781-
1782-
case opts.pretty do
1783-
true -> Inspect.Algebra.pretty(inspect(arg, opts), opts.width)
1784-
false -> Inspect.Algebra.pretty(inspect(arg, opts), :infinity)
1761+
opts = Inspect.Opts.new(opts)
1762+
limit = case opts.pretty do
1763+
true -> opts.width
1764+
false -> :infinity
17851765
end
1766+
Inspect.Algebra.pretty(Inspect.Algebra.to_doc(arg, opts), limit)
17861767
end
17871768

17881769
@doc """

lib/elixir/lib/range.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ defimpl Range.Iterator, for: Integer do
7373
end
7474

7575
defimpl Inspect, for: Range do
76+
import Inspect.Algebra
77+
7678
def inspect(Range[first: first, last: last], opts) do
77-
Inspect.Algebra.concat [Kernel.inspect(first, opts), "..", Kernel.inspect(last, opts)]
79+
concat [to_doc(first, opts), "..", to_doc(last, opts)]
7880
end
7981
end

0 commit comments

Comments
 (0)