Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These are called <strong>generalized type constraints</strong>. They allow you, from within a type-parameterized class or trait, to <em>further constrain</em> one of its type parameters. Here's an example:</p> <pre><code>case class Foo[A](a:A) { // 'A' can be substituted with any type // getStringLength can only be used if this is a Foo[String] def getStringLength(implicit evidence: A =:= String) = a.length } </code></pre> <p>The implicit argument <code>evidence</code> is supplied by the compiler, iff <code>A</code> is <code>String</code>. You can think of it as a <em>proof</em> that <code>A</code> is <code>String</code>--the argument itself isn't important, only knowing that it exists. <em>[edit: well, technically it actually is important because it represents an implicit conversion from <code>A</code> to <code>String</code>, which is what allows you to call <code>a.length</code> and not have the compiler yell at you]</em></p> <p>Now I can use it like so:</p> <pre><code>scala&gt; Foo("blah").getStringLength res6: Int = 4 </code></pre> <p>But if I tried use it with a <code>Foo</code> containing something other than a <code>String</code>:</p> <pre><code>scala&gt; Foo(123).getStringLength &lt;console&gt;:9: error: could not find implicit value for parameter evidence: =:=[Int,String] </code></pre> <p>You can read that error as "could not find evidence that Int == String"... that's as it should be! <code>getStringLength</code> is imposing <em>further restrictions</em> on the type of <code>A</code> than what <code>Foo</code> in general requires; namely, you can only invoke <code>getStringLength</code> on a <code>Foo[String]</code>. This constraint is enforced at compile-time, which is cool!</p> <p><code>&lt;:&lt;</code> and <code>&lt;%&lt;</code> work similarly, but with slight variations:</p> <ul> <li><code>A =:= B</code> means A must be exactly B</li> <li><code>A &lt;:&lt; B</code> means A must be a subtype of B (analogous to the <em>simple</em> type constraint <code>&lt;:</code>)</li> <li><code>A &lt;%&lt; B</code> means A must be <em>viewable</em> as B, possibly via implicit conversion (analogous to the simple type constraint <code>&lt;%</code>)</li> </ul> <p><a href="http://gist.github.com/229163" rel="noreferrer">This snippet</a> by @retronym is a good explanation of how this sort of thing used to be accomplished and how generalized type constraints make it easier now.</p> <p><strong>ADDENDUM</strong></p> <p>To answer your follow-up question, admittedly the example I gave is pretty contrived and not obviously useful. But imagine using it to define something like a <code>List.sumInts</code> method, which adds up a list of integers. You don't want to allow this method to be invoked on any old <code>List</code>, just a <code>List[Int]</code>. However the <code>List</code> type constructor can't be so constrainted; you still want to be able to have lists of strings, foos, bars, and whatnots. So by placing a generalized type constraint on <code>sumInts</code>, you can ensure that <em>just that method</em> has an additional constraint that it can only be used on a <code>List[Int]</code>. Essentially you're writing special-case code for certain kinds of lists.</p>
    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