2011-07-21 75 views
5
scala> implicitly[Int <:< AnyVal] 
res0: <:<[Int,AnyVal] = <function1> 

scala> class Foo 
defined class Foo 

scala> class Bar extends Foo 
defined class Bar 

scala> implicitly[Foo <:< Bar] 
<console>:8: error: could not find implicit value for parameter e: <:<[Foo,Bar] 
     implicitly[Foo <:< Bar] 
       ^

scala> implicitly[Bar <:< Foo] 
res2: <:<[Bar,Foo] = <function1> 

<:< kısıtlaması nasıl çalışır? Ya da daha kesin olarak, <:< örneklerini sağlayan örtülü tanım nerede?<: <İş nasıl?

+3

olası yinelenen [Ne oluies

+0

http://stackoverflow.com/questions/2603003/operator-in-scala – oluies

cevap

0

Predef nesnesinde bulunuyor.

scala> implicitly[Int <:< AnyVal] 
res1: <:<[Int,AnyVal] = <function1> 

scala> :type res1 
Predef$<:<[Int,AnyVal] 
8

Sen Predef 'da bulabilirsiniz.

implicit def conforms[A]: A <:< A = new (A <:< A) { def apply(x: A) = x } 

Aslında daha iyi anlayabilmeleri amacıyla kendi başınıza eklemenin deneyebilirsiniz: Örtülü yöntem conforms[A] Bu ipuçları sağlar ait

abstract class subclassOf[-From, +To] extends (From => To) 
implicit def subclassOfCheck[A]: A subclassOf A = new (A subclassOf A) { def apply(x: A) = x } 

implicitly[Int subclassOf AnyVal] 

class Foo 
class Bar extends Foo 

implicitly[Bar subclassOf Foo] 
İlgili konular