Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Using the <em>implicitly</em> method</strong></p> <p>The most common and general approach is to use the <a href="https://stackoverflow.com/questions/3855595/scala-function-implicitly">implicitly method</a>, defined in Predef:</p> <pre><code>def add[T: Numeric](x: T, y: T) = implicitly[Numeric[T]].plus(x,y) </code></pre> <p>Obviously, this is somewhat verbose and requires repeating the name of the type class. </p> <p><strong>Referencing the evidence parameter</strong> (<em>don't!</em>)</p> <p>Another alternative is to use the name of the implicit evidence parameter automatically generated by the compiler:</p> <pre><code>def add[T: Numeric](x: T, y: T) = evidence$1.plus(x,y) </code></pre> <p>It's surprising that this technique is even legal, and it should not be relied upon in practice since the name of the evidence parameter could change. </p> <p><strong>Context of a Higher Kind</strong> (<em>introducing the <code>context</code> method</em>)</p> <p>Instead, one can use a beefed-up version of the <code>implicitly</code> method. Note that the implicitly method is defined as</p> <pre><code>def implicitly[T](implicit e: T): T = e </code></pre> <p>This method simply relies on the compiler to insert an implicit object of the correct type from the surrounding scope into the method call, and then returns it. We can do a bit better:</p> <pre><code>def context[C[_], T](implicit e: C[T]) = e </code></pre> <p>This allows us to define our <code>add</code> method as</p> <pre><code>def add[T: Numeric](x: T, y: T) = context.plus(x,y) </code></pre> <p>The <code>context</code> method type parameters <code>Numeric</code> and <code>T</code> are inferred from the scope! Unfortunately, there are circumstances in which this <code>context</code> method will not work. When a type parameter has multiple context bounds or there are multiple parameters with different context bounds, for example. We can resolve the latter problem with a slightly more complex version:</p> <pre><code>class Context[T] { def apply[C[_]]()(implicit e: C[T]) = e } def context[T] = new Context[T] </code></pre> <p>This version requires us to specify the type parameter every time, but handles multiple type parameters.</p> <pre><code>def add[T: Numeric](x: T, y: T) = context[T]().plus(x,y) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload