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

<:< công việc hạn chế như thế nào? Hay chính xác hơn, đâu là định nghĩa ngầm định cung cấp các cá thể của <:<?<: <làm việc như thế nào?

+3

thể trùng lặp của [gì <: <, <% <, và =: = có nghĩa là trong Scala 2.8 và chúng được ghi chép ở đâu?] (http://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are -sey-documented) – oluies

+0

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

Trả lời

0

Nó nằm trong đối tượng Predef.

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

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

Bạn có thể tìm thấy số điện thoại trong Predef. phương pháp Implicit conforms[A] cung cấp những bằng chứng:

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

Bạn thực sự có thể cố gắng thực hiện điều đó cho mình để làm cho nó rõ ràng hơn:

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] 
Các vấn đề liên quan