Today diving deeper into making a friend out of the compiler.

We are going to learn on how to guarantee that a generic type follow specific rules. We are going to use *implicit proof*.

Hopefully this was not too hard. If you have any questions, please come ask them on the Discord server.

The first way to prove that a generic type `A`

is of type `T`

, we can do the following:

def name[A: T](arguments...): RETURN_TYPE = ???

This syntax will block compilation if we use the method with a type that cannot be proven to be of type `T`

or made of type `T`

with conversions.

You will often see this being used with `A : TypeTag: ClassTag`

but we are going to look into those Scala standard types an other time in a dedicated episode.

In our example, you can see that we define our proofs in `ValidFoos`

using the syntax:

implicit val name: PROOF_TYPE[TYPE_NEEDING_PROOF] = ???

Our example is of course a bit silly but there are production use cases that would need those structures. For instance if you have complex data types that share underlying `trait`

. But we are going to see more advance use case right now, when we actually use the proof.

In the second example, not only we are requiring a proof, but we are not using the `[A: T]`

syntax, we are using the syntax that expose the proof so we can use it:

def name[A](arguments...)(implicit proof_name: PROOF_TYPE[A]): RETURN_TYPE = ???

Written this way, we can actually use the proof and do things with it.

In this case, we want the proof that the input is some kind of number, we do not want to write one function for `Int`

, one for `Double`

, one for `Long`

, etcâ€¦ That would be very tedious. So we need a way to target all numbers. In Scala, we can prove that a TYPE is a number when there a `Numeric[A]`

proof.

Once we acquire the proof, we can use `num`

to perform operations. For instance, we simply perform a sum here.

Reveal more information and clues