Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-release > by-pkgid > 016232f1d9a3f7bee85855d35a2bca58 > files > 77

elixir-doc-1.7.2-1.mga7.noarch.rpm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="ExDoc v0.19.1">
    <title>Inspect – Elixir v1.7.2</title>
    <link rel="stylesheet" href="dist/app-240d7fc7e5.css" />
      <link rel="canonical" href="https://hexdocs.pm/elixir/v1.7/Inspect.html" />
    <script src="dist/sidebar_items-cdf4e58b19.js"></script>
    
  </head>
  <body data-type="modules">
    <script>try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { }</script>
    <div class="main">
<button class="sidebar-button sidebar-toggle">
  <span class="icon-menu" aria-hidden="true"></span>
  <span class="sr-only">Toggle Sidebar</span>
</button>
<button class="sidebar-button night-mode-toggle">
  <span class="icon-theme" aria-hidden="true"></span>
  <span class="sr-only">Toggle Theme</span>
</button>
<section class="sidebar">

  <a href="http://elixir-lang.org/docs.html" class="sidebar-projectLink">
    <div class="sidebar-projectDetails">
      <h1 class="sidebar-projectName">
Elixir      </h1>
      <h2 class="sidebar-projectVersion">
        v1.7.2
      </h2>
    </div>
      <img src="assets/logo.png" alt="Elixir" class="sidebar-projectImage">
  </a>

  <form class="sidebar-search" action="search.html">
    <button type="submit" class="search-button">
      <span class="icon-search" aria-hidden="true"></span>
    </button>
    <input name="q" type="text" id="search-list" class="search-input" placeholder="Search" aria-label="Search" autocomplete="off" />
  </form>

  <ul class="sidebar-listNav">
    <li><a id="extras-list" href="#full-list">Pages</a></li>

      <li><a id="modules-list" href="#full-list">Modules</a></li>

      <li><a id="exceptions-list" href="#full-list">Exceptions</a></li>

  </ul>
  <div class="gradient"></div>
  <ul id="full-list" class="sidebar-fullList"></ul>
</section>

<section class="content">
  <div class="content-outer">
    <div id="content" class="content-inner">


      <h1>
        <small class="visible-xs">Elixir v1.7.2</small>
Inspect <small>protocol</small>        
          <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/inspect.ex#L6" title="View Source" class="view-source" rel="help">
            <span class="icon-code" aria-hidden="true"></span>
            <span class="sr-only">View Source</span>
          </a>
      </h1>


        <section id="moduledoc">
<p>The <a href="Inspect.html#content"><code class="inline">Inspect</code></a> protocol converts an Elixir data structure into an
algebra document.</p>
<p>This documentation refers to implementing the <a href="Inspect.html#content"><code class="inline">Inspect</code></a> protocol
for your own data structures. To learn more about using inspect,
see <a href="Kernel.html#inspect/2"><code class="inline">Kernel.inspect/2</code></a> and <a href="IO.html#inspect/2"><code class="inline">IO.inspect/2</code></a>.</p>
<p>The <a href="#inspect/2"><code class="inline">inspect/2</code></a> function receives the entity to be inspected
followed by the inspecting options, represented by the struct
<a href="Inspect.Opts.html"><code class="inline">Inspect.Opts</code></a>. Building of the algebra document is done with
<a href="Inspect.Algebra.html"><code class="inline">Inspect.Algebra</code></a>.</p>
<h2 id="module-examples" class="section-heading">
  <a href="#module-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<p>Many times, inspecting a structure can be implemented in function
of existing entities. For example, here is <a href="MapSet.html"><code class="inline">MapSet</code></a>’s <code class="inline">inspect</code>
implementation:</p>
<pre><code class="nohighlight makeup elixir"><span class="kd">defimpl</span><span class="w"> </span><span class="nc">Inspect</span><span class="p">,</span><span class="w"> </span><span class="ss">for</span><span class="p">:</span><span class="w"> </span><span class="nc">MapSet</span><span class="w"> </span><span class="k" data-group-id="1746723684-1">do</span><span class="w">
  </span><span class="kn">import</span><span class="w"> </span><span class="nc">Inspect.Algebra</span><span class="w">

  </span><span class="kd">def</span><span class="w"> </span><span class="nf">inspect</span><span class="p" data-group-id="1746723684-2">(</span><span class="n">dict</span><span class="p">,</span><span class="w"> </span><span class="n">opts</span><span class="p" data-group-id="1746723684-2">)</span><span class="w"> </span><span class="k" data-group-id="1746723684-3">do</span><span class="w">
    </span><span class="n">concat</span><span class="p" data-group-id="1746723684-4">(</span><span class="p" data-group-id="1746723684-5">[</span><span class="s">&quot;#MapSet&lt;&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">to_doc</span><span class="p" data-group-id="1746723684-6">(</span><span class="nc">MapSet</span><span class="o">.</span><span class="n">to_list</span><span class="p" data-group-id="1746723684-7">(</span><span class="n">dict</span><span class="p" data-group-id="1746723684-7">)</span><span class="p">,</span><span class="w"> </span><span class="n">opts</span><span class="p" data-group-id="1746723684-6">)</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;&gt;&quot;</span><span class="p" data-group-id="1746723684-5">]</span><span class="p" data-group-id="1746723684-4">)</span><span class="w">
  </span><span class="k" data-group-id="1746723684-3">end</span><span class="w">
</span><span class="k" data-group-id="1746723684-1">end</span></code></pre>
<p>The <code class="inline">concat/1</code> function comes from <a href="Inspect.Algebra.html"><code class="inline">Inspect.Algebra</code></a> and it
concatenates algebra documents together. In the example above,
it is concatenating the string <code class="inline">&quot;MapSet&lt;&quot;</code> (all strings are
valid algebra documents that keep their formatting when pretty
printed), the document returned by <a href="Inspect.Algebra.html#to_doc/2"><code class="inline">Inspect.Algebra.to_doc/2</code></a> and the
other string <code class="inline">&quot;&gt;&quot;</code>.</p>
<p>Since regular strings are valid entities in an algebra document,
an implementation of inspect may simply return a string,
although that will devoid it of any pretty-printing.</p>
<h2 id="module-error-handling" class="section-heading">
  <a href="#module-error-handling" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Error handling
</h2>

<p>In case there is an error while your structure is being inspected,
Elixir will raise an <a href="ArgumentError.html"><code class="inline">ArgumentError</code></a> error and will automatically fall back
to a raw representation for printing the structure.</p>
<p>You can however access the underlying error by invoking the Inspect
implementation directly. For example, to test Inspect.MapSet above,
you can invoke it as:</p>
<pre><code class="nohighlight makeup elixir"><span class="nc">Inspect.MapSet</span><span class="o">.</span><span class="n">inspect</span><span class="p" data-group-id="3015875645-1">(</span><span class="nc">MapSet</span><span class="o">.</span><span class="n">new</span><span class="p" data-group-id="3015875645-2">(</span><span class="p" data-group-id="3015875645-2">)</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="3015875645-3">%</span><span class="nc" data-group-id="3015875645-3">Inspect.Opts</span><span class="p" data-group-id="3015875645-3">{</span><span class="p" data-group-id="3015875645-3">}</span><span class="p" data-group-id="3015875645-1">)</span></code></pre>
        </section>

        <section id="summary" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#summary">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Summary
          </h1>
  <div class="summary-types summary">
    <h2>
      <a href="#types">Types</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:t/0">t()</a>
  </div>
</div>
  </div>
          
  <div class="summary-functions summary">
    <h2>
      <a href="#functions">Functions</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#inspect/2">inspect(term, opts)</a>
  </div>
</div>
  </div>
          
        </section>

        <section id="types" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#types">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Types
          </h1>
          <div class="types-list">
<div class="detail" id="t:t/0">
    <div class="detail-header">
    <a href="#t:t/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">t()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/inspect.ex#L6" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>t() :: <a href="typespecs.html#built-in-types">term</a>()</pre>
      </div>
  </div>
  <section class="docstring">
  </section>
</div>
          </div>
        </section>


        <section id="functions" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#functions">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Functions
          </h1>
<div class="detail" id="inspect/2">
    <div class="detail-header">
    <a href="#inspect/2" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">inspect(term, opts)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/inspect.ex#L62" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
          </div>
  <section class="docstring">
  </section>
</div>
        </section>

          <footer class="footer">
        <p>
          <span class="line">
            Built using
            <a href="https://github.com/elixir-lang/ex_doc" title="ExDoc" target="_blank" rel="help noopener">ExDoc</a> (v0.19.1),
          </span>
          <span class="line">
            designed by
            <a href="https://twitter.com/dignifiedquire" target="_blank" rel="noopener" title="@dignifiedquire">Friedel Ziegelmayer</a>.
            </span>
        </p>
      </footer>
    </div>
  </div>
</section>
</div>
  <script src="dist/app-a0c90688fa.js"></script>
  
  </body>
</html>