代码之家  ›  专栏  ›  技术社区  ›  scout

scala中的<:<运算符

  •  38
  • scout  · 技术社区  · 16 年前

    <:< scala中的运算符。

    if(apple <:< fruit)  //checks if apple is a subclass of fruit.
    

    还有其他解释吗?我在scala源文件中看到许多定义。

    7 回复  |  直到 16 年前
        1
  •  26
  •   oxbow_lakes    16 年前

    <:< 不是操作员 -它是一个 标识符 因此是:

    • 一个
    • 方法/val或var的名称

    < 在库中出现两次,在 Predef 一次作为类,一次作为方法 Manifest .

    对于上的方法 显示 manifest是由manifest参数表示的子类型。

    对于输入 Predef公司

    class <%<[-From, +To] extends (From) ⇒ To
    class <:<[-From, +To] extends (From) ⇒ To
    class =:=[From, To] extends (From) ⇒ To
    
        2
  •  31
  •   Ben Lings    16 年前

    这个 <:< 类型在中定义 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)
    

    这使用了一个泛型类型的Scala特性 op[T1, T2] 可以写 T1 op T2 toMap 方法只能用于 Traversable Tuple2 ). 如注释中所述,这将泛化普通泛型类型约束,以允许它引用任何范围内的抽象类型/类型参数。使用这个( implicit ev : T1 <:< T2 )比简单地使用证据参数( implicit ev: T1 => T2

    我肯定在一个Scala邮件列表上看到过一些关于这个的讨论,但是现在找不到。

        3
  •  14
  •   aioobe    16 年前

    我四处打听,得到的解释是:

    <:< TraversableOnce , toMap 声明为 def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] 托马普 方法仅在可遍历包含2元组时有效。 flatten 这是另一个例子。 <:< 用于表示只能展平可遍历对象的可遍历对象的约束。

        4
  •  7
  •   Daniel C. Sobral    16 年前

    实际上,它检查类 代表 Manifest apple是由manifest fruit表示的类的一个子类。

    例如:

    manifest[java.util.List[String]] <:< manifest[java.util.ArrayList[String]] == false
    manifest[java.util.ArrayList[String]] <:< manifest[java.util.List[String]] == true
    
        5
  •  3
  •   Eastsun    16 年前

    从scala.Predef.scala复制:

    // Type Constraints --------------------------------------------------------------
    
      // 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}
    
        6
  •  2
  •   Community Mohan Dere    9 年前

    implementation .

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

    我试图设计一个更简单的实现。以下操作不起作用。

    sealed class <:<[-From <: To, +To]
    implicit def conforms[A <: B, B]: A <:< B = new (A <:< B)
    

    至少因为它根本不会打签入 valid use 案例。

    case class L[+A]( elem: A )
    {
       def contains[B](x: B)(implicit ev: A <:< B) = elem == x
    }
    
    error: type arguments [A,B] do not conform to class <:<'s
           type parameter bounds [-From <: To,+To]
    def contains[B](x: B)(implicit ev: A <:< B) = elem == x
                                         ^
    
        7
  •  1
  •   DVK    16 年前

    http://jim-mcbeath.blogspot.com/2008/09/scala-syntax-primer.html#types

    List[T] forSome { type T <: Component }
    

    在上面的例子中,我们说T是某个类型,它是组件的一个子类型。

        8
  •  0
  •   Evgeniy Sobolev    5 年前

      /**
       * An instance of `A <:< B` witnesses that `A` is a subtype of `B`.
       * Requiring an implicit argument of the type `A <:< B` encodes
       * the generalized constraint `A <: B`.
       *
       * @note we need a new type constructor `<:<` and evidence `conforms`,
       * as reusing `Function1` and `identity` leads to ambiguities in
       * case of type errors (`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; or for lower-bounds, use: `L <:< T`, where `L` is the
       * required lower bound.
       *
       * In part contributed by Jason Zaugg.
       */
      @implicitNotFound(msg = "Cannot prove that ${From} <:< ${To}.")
      sealed abstract class <:<[-From, +To] extends (From => To) with Serializable
      private[this] final val singleton_<:< = new <:<[Any,Any] { def apply(x: Any): Any = x }
      // The dollar prefix is to dodge accidental shadowing of this method
      // by a user-defined method of the same name (SI-7788).
      // The collections rely on this method.
      implicit def $conforms[A]: A <:< A = singleton_<:<.asInstanceOf[A <:< A]
    
      @deprecated("Use `implicitly[T <:< U]` or `identity` instead.", "2.11.0")
      def conforms[A]: A <:< A = $conforms[A]
    
      /** An instance of `A =:= B` witnesses that the types `A` and `B` are equal.
       *
       * @see `<:<` for expressing subtyping constraints
       */
      @implicitNotFound(msg = "Cannot prove that ${From} =:= ${To}.")
      sealed abstract class =:=[From, To] extends (From => To) with Serializable
      private[this] final val singleton_=:= = new =:=[Any,Any] { def apply(x: Any): Any = x }
      object =:= {
         implicit def tpEquals[A]: A =:= A = singleton_=:=.asInstanceOf[A =:= A]
      }
    
    推荐文章