Understanding Multiple Boundaries of Context

Looking at the spire method signature:

implicit def complex[A: Fractional: Trig: IsReal: Dist]: Dist[Complex[A]]

What is the point [A: Fractional: Trig ...]?

+4
source share
2 answers

A context constraint is a way of asserting the existence of an implicit value. For example, the signature of a method:

def complex[A : Fractional]

Means that when a method is called, a type value Fractional[A]is available that is available in the scope (so that the body of the method can be used implicitly[Fractional[A]]to get an instance of this type). Compilation will fail if the compiler has no evidence of this fact. The boundaries of the context are really syntactic sugar, so the above method signature is equivalent:

def complex[A](implicit ev: Fractional[A])

, :

def complex[A : Fractional : Trig]

, Fractional[A] Trig[A]. , :

def complex[A](implicit ev0: Fractional[A], ev1: Trig[A])

REPL, , :

trait Foo[A]
trait Bar[A]
def foo[A : Foo : Bar] = ???
// foo: [A](implicit evidence$1: Foo[A], implicit evidence$2: Bar[A])Nothing
+8

A. , , , Fractional[A], Trig[A] .. , Fractional - . , , , Double s, Double - .

0

All Articles