In Scala 3 type-evidence (sometimes called “type-level proofs”) is just a way to require, at compile time, that certain relationships between types hold. You do it by asking the compiler to provide you with a “proof” (an implicit value) of one of these relationships:
• A =:= B – evidence that A and B are the same type
• A <:< B – evidence that A is a subtype of B
• custom “type classes” or GADTs to prove your own relations
Under the hood these are just traits in scala.Predef with given instances that the compiler can summon automatically.
1. Built-in evidence: =:= and <:<
=:= (type equality)
scala
def mustBeSame[A, B](using ev: A =:= B): String =
// Because ev: A =:= B, we can treat an A as a B
val proof: B = ev.apply( summon[A](??? ) )
s"A and B are the same. Got proof: $proof"
@main def testEq() =
println(mustBeSame[Int, Int]) // compiles
// println(mustBeSame[Int, String]) // error: no given A =:= B
<:< (subtyping)
scala
def acceptSuper[T, U](t: T)(using ev: T <:< U): U =
ev(t) // upcast T to U
@main def testSub() =
val s: String = "hello"
println(acceptSuper[String, Any](s)) // OK: String <:< Any
// println(acceptSuper[Any, String](42)) // error: Any <:< String not found
Under the covers, =:= and <:< are declared like:
scala
sealed abstract class =:=[A, B] { def apply(a: A): B }
object =:= {
given refl[A]: A =:= A = new =:=[A, A] { def apply(a: A) = a }
}
sealed abstract class <:<[-A, +B] { def apply(a: A): B }
object <:< {
given refl[A]: A <:< A = new <:<[A, A] { def apply(a: A) = a }
}
The compiler automatically provides these “reflexive” givens (refl) and can coerce them to other contexts (e.g. if String <:< Any, it will use the refl on Any).
2. Summoning evidence
Whenever you need to grab the proof manually, use summon (formerly implicitly):
scala
def foo[A, B](using ev: A =:= B) =
val same: A = summon[A =:= B].apply(summon[A])
println(same)
You’re not limited to the built-ins. For example, suppose you want evidence that List[A] and List[B] have the same element type:
scala
trait ElemEq[L1[_], L2[_]] {
type T
def convert: L1[T] => L2[T]
}
object ElemEq {
given sameElem[F[_], G[_], X]: ElemEq[F, G] with {
type T = X
def convert = (fx: F[X]) =>
// imagine a conversion from F[X] to G[X]
???
}
}
// usage
def convertLists[F[_], G[_]](fs: F[Int])
(using eq: ElemEq[F, G] { type T = Int }): G[Int] =
eq.convert(fs)
The compiler can summon your custom ElemEq just like =:= or <:<.
4. When to use type-evidence
• API constraints
Enforce that a method only compiles when certain type relationships hold.
• Generic derivation
Build up proofs step by step (e.g. Shapeless-style) to derive codecs, serializers, etc.
• Type-safe DSLs
Prevent invalid states by ruling out illegal combinations at compile time.
5. Recap
=:= proves equality
<:< proves subtyping
summon fetches a given evidence
You can define your own evidence traits and give them given instances
Inline and scala.compiletime tools let you make these checks zero-overhead
With these building blocks, you can embed rich, compile-time checked invariants in your Scala 3 code. 🚀
User
Can you also explain type lambdas in that context?
Assistant
In Scala 3 a “type-lambda” is simply an anonymous, higher-kinded type function—i.e. a way to write “partially applied” type constructors on the fly, so you can pass them to methods or summon givens that expect an F[_], F[_, _], etc.
1. Syntax
• For a single type parameter: scala [X] =>> …body using X…
• For two type parameters: scala [X, Y] =>> …body using X and Y…
Under the hood, [X] =>> T[X] is just sugar for
scala
trait Lambda { type λ[X] = T[X] }
but much more concise.
2. Why we need them
Imagine you have a generic method
scala
def build[F[_]: Functor]: Foo[F] = …
but your “F” is really Either[String, *] (i.e. X =>> Either[String, X]). You can’t write
scala
build[Either[String, _]]
in Scala 3—so you use a type-lambda:
scala
build[[X] =>> Either[String, X]]
3. Common examples
3.1 Partially apply a 2-param type
scala
// Want: G[Y] = Either[String, Y]
type ErrorOr[Y] = Either[String, Y]
// or inline:
build[[Y] =>> Either[String, Y]]
3.2 Flip parameters
Say you have Map[K, V] but want something of kind * -> *:
If you want evidence between, say, Either[String, *] and Option[*], you’d write:
scala
summon[ElemEq[[X] =>> Either[String, X], Option]]
Or if you have a generic equality proof:
scala
summon[[X] =>> List[X] =:= [Y] =>> Seq[Y]]
You can even define givens on such anonymous lambdas:
scala
given eqEitherOption[Int]: Either[String, Int] =:= Option[Int] = ???
// then:
summon[[X] =>> Either[String, X] =:= Option] // uses the above at X = Int
5. Putting it all together
scala
// A “higher-kinded” evidence that F[_] and G[_] share the same element type
trait ElemEq[F[_], G[_]] {
type T
def convert: F[T] => G[T]
}
object ElemEq {
// Universal instance—but you could constrain T if you like
given sameElem[F[_], G[_], X]: ElemEq[F, G] with {
type T = X
def convert = fx =>
// pretend‐conversion
println(s"Converting $fx…"); sys.error("not implemented")
}
}
// Later…
def demo =
// summon evidence between a type-lambda and a normal type constructor
val ev = summon[ElemEq[[X] =>> Either[String, X], Option]]
// ev.T is inferred by the compiler
println(s"Inferred element type: ${ev.T}")
Takeaways
Type-lambdas ([X] =>> …) let you write anonymous F[], F[, _], etc.
They’re essential when you want to feed a “partially applied” type into
a type-class, a type-evidence trait, or any HKD.
Combined with summon, using and your custom givens, you get the power
to express very rich, compile-time-checked invariants. 🚀