Scala type signs

In the scala source, I can see this code:

@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 }
// not in the <:< companion object because it is also
// intended to subsume identity (which is no longer implicit)
implicit def conforms[A]: A <:< A = singleton_<:<.asInstanceOf[A <:< A]

@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]
}

It’s not entirely clear to me why both of these classes are expanding Function1. Will something spread enough?

+2
source share
1 answer

In most cases, when you want to make sure that A <:< B(or, rather, you want to make sure that you are A <: Busing type proof A <:< B), this is because in fact you have a type value Aand want to be able to treat it as an instance of the type B.

x: A, , A B, x B.

<:< , , ( , ). , A <:< B, , , A B ( x:A B).

, , , , <:< Function1 .

=:=.

UPDATE: " =: =, A A?":

, <:< : , A <: B, A B ( ). , :

class Foo { 
  def hello() { println("hello!") } 
}
def f[T]( value: T )(implicit e: T <:< Foo){
  value.hello()
}
class Bar extends Foo
f( new Bar )

f , value T. , T Foo. , , e: T <:< Foo T Foo, value.hello() , T - , - . e: T <:< Foo , T <: Foo. , T Foo . , T Foo, T <:< Foo.

, =:=: T =:= Foo T = Foo, .

+6

All Articles