What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?

后端 未结 4 1615
野性不改
野性不改 2020-11-22 13:26

I can see in the API docs for Predef that they\'re subclasses of a generic function type (From) => To, but that\'s all it says. Um, what? Maybe there\'s documentation some

4条回答
  •  孤独总比滥情好
    2020-11-22 14:23

    It depends on where they are being used. Most often, when used while declaring types of implicit parameters, they are classes. They can be objects too in rare instances. Finally, they can be operators on Manifest objects. They are defined inside scala.Predef in the first two cases, though not particularly well documented.

    They are meant to provide a way to test the relationship between the classes, just like <: and <% do, in situations when the latter cannot be used.

    As for the question "when should I use them?", the answer is you shouldn't, unless you know you should. :-) EDIT: Ok, ok, here are some examples from the library. On Either, you have:

    /**
      * Joins an Either through Right.
      */
     def joinRight[A1 >: A, B1 >: B, C](implicit ev: B1 <:< Either[A1, C]): Either[A1, C] = this match {
       case Left(a)  => Left(a)
       case Right(b) => b
     }
    
     /**
      * Joins an Either through Left.
      */
     def joinLeft[A1 >: A, B1 >: B, C](implicit ev: A1 <:< Either[C, B1]): Either[C, B1] = this match {
       case Left(a)  => a
       case Right(b) => Right(b)
     }
    

    On Option, you have:

    def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null
    

    You'll find some other examples on the collections.

提交回复
热议问题