任何人都可以<:<在scala中提供有关运算符的一些细节.我认为:
if(apple <:< fruit) //checks if apple is a subclass of fruit.
Run Code Online (Sandbox Code Playgroud)
还有其他解释吗?我在scala源文件中看到了很多定义.
Ben*_*ngs 31
该<:<类型在Predef.scala中定义以及相关类型=:=,<%<如下所示:
// used, for example, in the encoding of generalized constraints
// we need a new type constructor `<:<` and evidence `conforms`, as
// reusing `Function2` and `identity` leads to ambiguities (any2stringadd is inferred)
// to constrain any abstract type T that's in scope in a method's argument list (not just the method's own type parameters)
// simply add an implicit argument of type `T <:< U`, where U is the required upper bound (for lower-bounds, use: `U <: T`)
// in part contributed by Jason Zaugg
sealed abstract class <:<[-From, +To] extends (From => To)
implicit def conforms[A]: A <:< A = new (A <:< A) {def apply(x: A) = x} // not in the <:< companion object because it is also intended to subsume identity (which is no longer implicit)
Run Code Online (Sandbox Code Playgroud)
这使用Scala功能,op[T1, T2]可以编写泛型类型T1 op T2.正如aioobe所指出的,这可以用于为仅适用于泛型类型的某些实例的方法提供证据参数(给出的示例是toMap仅可用于a Traversable的方法Tuple2).如注释中所述,这概括了一个普通的泛型类型约束,以允许它引用任何范围内的抽象类型/类型参数.使用this(implicit ev : T1 <:< T2)具有优于简单地使用像(implicit ev: T1 => T2)这样的证据参数的优点,因为后者可能导致用于转换的意外范围内隐式值.
我确定我在其中一个Scala邮件列表上看到了一些关于此问题的讨论,但目前还没有找到.
oxb*_*kes 26
<:<是不是一个操作符 -它是一个标识符,并因此是以下之一:
在这种情况下,<:<在库中出现两次,一次Predef作为一个类,一次作为一个方法Manifest.
对于方法on Manifest,它检查此清单表示的类型是否是manifest参数表示的类型.
对于类型Predef,这是相对较新的,我也有点困惑,因为它似乎是三个相同的声明的一部分!
class <%<[-From, +To] extends (From) ? To
class <:<[-From, +To] extends (From) ? To
class =:=[From, To] extends (From) ? To
Run Code Online (Sandbox Code Playgroud)
aio*_*obe 14
我四处询问,这是我得到的解释:
<:<通常用作证据参数.例如TraversableOnce,toMap声明为def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U].这表示该toMap方法仅在遍历包含2元组时才有效的约束.flatten是另一个例子.<:<用于表示您只能展平可遍历遍历的约束.
实际上,它检查类代表由Manifest苹果是清单水果代表的类的子类.
例如:
manifest[java.util.List[String]] <:< manifest[java.util.ArrayList[String]] == false
manifest[java.util.ArrayList[String]] <:< manifest[java.util.List[String]] == true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7189 次 |
| 最近记录: |