2011-07-21 21 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> 

<:<はどのように機能しますか?より正確には、<:<のインスタンスを提供する暗黙の定義はどこですか?<:<はどのように動作しますか?

+3

[<何の可能重複:<、<%<を、 Scala 2.8では=と=の意味があり、どこに文書化されていますか?](http://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are -they-documentsed) – oluies

+0

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

答えて

0

Predefオブジェクトにあります。

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

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

あなたはPredefでそれを見つけることができます。暗黙の方法conforms[A]は、これらの証拠を提供します。

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

あなたが実際にそれをより明確にするために、それを自分で実装しようとすることができます

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] 
関連する問題