gpt4 book ai didi

scala - < :< work? 怎么样

转载 作者:行者123 更新时间:2023-12-04 17:56:54 25 4
gpt4 key购买 nike

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>

怎么样 <:<约束工作?或者更准确地说,提供 <:< 实例的隐式定义在哪里? ?

最佳答案

您可以在 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]

关于scala - < :< work? 怎么样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6778009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com