Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>1. def bar[A &lt;: java.io.Serializable](i:A) = i </code></pre> <p>&lt;: - guarantees that instance of <strong>i</strong> of type parameter <strong>A</strong> will be subtype of <strong>Serializable</strong></p> <pre><code>2. def foo[A](i:A)(implicit ev : A &lt;:&lt; java.io.Serializable) = i </code></pre> <p>&lt;:&lt; - guarantees that execution context will contains implicit value (for <strong>ev</strong> paramenter) of type <strong>A</strong> what is subtype of <strong>Serializable</strong>. This implicit defined in Predef.scala and for <strong><em>foo</em></strong> method and it is prove if instance of type parameter A is subtype of <strong>Serializable</strong>: </p> <pre><code>implicit def conforms[A]: A &lt;:&lt; A = singleton_&lt;:&lt;.asInstanceOf[A &lt;:&lt; A] </code></pre> <p>fictional case of using &lt;:&lt; operator:</p> <pre><code>class Boo[A](x: A) { def get: A = x def div(implicit ev : A &lt;:&lt; Double) = x / 2 def inc(implicit ev : A &lt;:&lt; Int) = x + 1 } val a = new Boo("hi") a.get // - OK a.div // - compile time error String not subtype of Double a.inc // - compile tile error String not subtype of Int val b = new Boo(10.0) b.get // - OK b.div // - OK b.inc // - compile time error Double not subtype of Int val c = new Boo(10) c.get // - OK c.div // - compile time error Int not subtype of Double c.inc // - OK </code></pre> <ul> <li>if we not call methods what not conform &lt;:&lt; condition than all compile and execute.</li> </ul>
 

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