Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yet another solution is to specify a default type for the parameter as follows:</p> <pre><code>object Finder { def find[T &lt;: Node](name: String)(implicit e: T DefaultsTo Node): T = doFind(name).asInstanceOf[T] } </code></pre> <p>The key is to define the following <em>phantom type</em> to act as a witness for the default:</p> <pre><code>sealed class DefaultsTo[A, B] trait LowPriorityDefaultsTo { implicit def overrideDefault[A,B] = new DefaultsTo[A,B] } object DefaultsTo extends LowPriorityDefaultsTo { implicit def default[B] = new DefaultsTo[B, B] } </code></pre> <p>The advantage of this approach is that it avoids the error altogether (at both run-time and compile-time). If the caller does not specify the type parameter, it defaults to <code>Node</code>.</p> <p><strong>Explanation</strong>:</p> <p>The signature of the <code>find</code> method ensures that it can only be called if the caller can supply an object of type <code>DefaultsTo[T, Node]</code>. Of course, the <code>default</code> and <code>overrideDefault</code> methods make it easy to create such an object for any type <code>T</code>. Since these methods are implicit, the compiler automatically handles the business of calling one of them and passing the result into <code>find</code>.</p> <p><em>But how does the compiler know which method to call?</em> It uses its type inference and implicit resolution rules to determine the appropriate method. There are three cases to consider:</p> <ol> <li><p><code>find</code> is called with no type parameter. In this case, type <code>T</code> must be inferred. Searching for an implicit method that can provide an object of type <code>DefaultsTo[T, Node]</code>, the compiler finds <code>default</code> and <code>overrideDefault</code>. <code>default</code> is chosen since it has priority (because it's defined in a proper subclass of the trait that defines <code>overrideDefault</code>). As a result, <code>T</code> must be bound to <code>Node</code>.</p></li> <li><p><code>find</code> is called with a non-<code>Node</code> type parameter (e.g., <code>find[MyObj]("name")</code>). In this case, an object of type <code>DefaultsTo[MyObj, Node]</code> must be supplied. Only the <code>overrideDefault</code> method can supply it, so the compiler inserts the appropriate call.</p></li> <li><p><code>find</code> is called with <code>Node</code> as the type parameter. Again, either method is applicable, but <code>default</code> wins due to its higher priority.</p></li> </ol>
    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