An instance ofA <:< B witnesses thatA is a subtype ofB. Requiring an implicit argument of the typeA <:< B encodes the generalized constraintA <: B.
To constrain any abstract typeT that's in scope in a method's argument list (not just the method's own type parameters) simply add an implicit argument of typeT <:< U, whereU is the required upper bound; or for lower-bounds, use:L <:< T, whereL is the required lower bound.
In case of any confusion over which method goes in what direction, all the "Co" methods (includingapply) go from left to right in the type ("with" the type), and all the "Contra" methods go from right to left ("against" the type). E.g.,apply turns aFrom into aTo, andsubstituteContra replaces theTos in a type withFroms.
In part contributed by Jason Zaugg.
a type which is proved a subtype ofTo
a type which is proved a supertype ofFrom
=:= for expressing equality constraints
sealed trait Option[+A] { // def flatten[B, A <: Option[B]]: Option[B] = ... // won't work, since the A in flatten shadows the class-scoped A. def flatten[B](implicit ev: A <:< Option[B]): Option[B] = if(isEmpty) None else ev(get) // Because (A <:< Option[B]) <: (A => Option[B]), ev can be called to turn the // A from get into an Option[B], and because ev is implicit, that call can be // left out and inserted automatically.}SubstituteTo forFrom andFrom forTo in the typeF[To, From], given thatF is contravariant in the first argument and covariant in the second.
SubstituteTo forFrom andFrom forTo in the typeF[To, From], given thatF is contravariant in the first argument and covariant in the second. Essentially swapsTo andFrom inftf's type.
Equivalent in power to each ofsubstituteCo andsubstituteContra.
This method is impossible to implement withoutthrowing or otherwise "cheating" unlessFrom <: To, so it ensures that this really represents a subtyping relationship.
ftf, but with a (potentially) different type
Composes two instances ofFunction1 in a newFunction1, with this function applied first.
Composes two instances ofFunction1 in a newFunction1, with this function applied first.
the result type of functiong
a function R => A
a new functionf such thatf(x) == g(apply(x))
IfFrom <: To andTo <: C, thenFrom <: C (subtyping is transitive)
IfFrom <: To andTo <: C, thenFrom <: C (subtyping is transitive)
Coerce aFrom into aTo.
Coerce aFrom into aTo. This is guaranteed to be the identity function.
This method is often called implicitly as an implicitA <:< B doubles as an implicit viewA => B.
some value of typeFrom
f, but with a (potentially) different type
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
the type to which functiong can be applied
a function A => T1
a new functionf such thatf(x) == apply(g(x))
IfFrom <: To andC <: From, thenC <: To (subtyping is transitive)
IfFrom <: To andC <: From, thenC <: To (subtyping is transitive)
Lift this evidence over a covariant type constructorF.
Lift this evidence over a contravariant type constructorF.
Substitute theFrom in the typeF[From], whereF is a covariant type constructor, forTo.
Substitute theFrom in the typeF[From], whereF is a covariant type constructor, forTo.
Equivalent in power to each ofsubstituteBoth andsubstituteContra.
This method is impossible to implement withoutthrowing or otherwise "cheating" unlessFrom <: To, so it ensures that this really represents a subtyping relationship.
ff, but with a (potentially) different type
Substitute theTo in the typeF[To], whereF is a contravariant type constructor, forFrom.
Substitute theTo in the typeF[To], whereF is a contravariant type constructor, forFrom.
Equivalent in power to each ofsubstituteBoth andsubstituteCo.
This method is impossible to implement withoutthrowing or otherwise "cheating" unlessFrom <: To, so it ensures that this really represents a subtyping relationship.
ft, but with a (potentially) different type
Returns a string representation of the object.
Returns a string representation of the object.
The default representation is platform dependent.
a string representation of the object.