Skip to main content
deleted 130 characters in body
Source Link
Erik Kaplun
  • 38.5k
  • 15
  • 102
  • 113

Context bounds desugar into implicit parameters, e.g. consider a function that leverages the Monoid type class:

def suml[T: Monoid](xs: List[T]) = { val T = implicitly[Monoid[T]] xs.foldLeft(T.mzero)(T.mplus) } 

where the : Monoid part is a context bound, gets translated to:

def suml[T](xs: List[T])(implicit evidence$1: Monoid[T]]) = { ... } 

therefore the following compiles, too:

def suml[T: Monoid](xs: List[T]) = { val T = evidence$1 ... } 

(note that the name T in val T is chosen arbitrarily to match the type T but bears no lexical association with type T)

Context bounds desugar into implicit parameters, e.g. consider a function that leverages the Monoid type class:

def suml[T: Monoid](xs: List[T]) = { val T = implicitly[Monoid[T]] xs.foldLeft(T.mzero)(T.mplus) } 

where the : Monoid part is a context bound, gets translated to:

def suml[T](xs: List[T])(implicit evidence$1: Monoid[T]]) = { ... } 

therefore the following compiles, too:

def suml[T: Monoid](xs: List[T]) = { val T = evidence$1 ... } 

(note that the name T in val T is chosen arbitrarily to match the type T but bears no lexical association with type T)

Context bounds desugar into implicit parameters, e.g. consider a function that leverages the Monoid type class:

def suml[T: Monoid](xs: List[T]) = { val T = implicitly[Monoid[T]] xs.foldLeft(T.mzero)(T.mplus) } 

where the : Monoid part is a context bound, gets translated to:

def suml[T](xs: List[T])(implicit evidence$1: Monoid[T]]) = { ... } 

therefore the following compiles, too:

def suml[T: Monoid](xs: List[T]) = { val T = evidence$1 ... } 
Source Link
Erik Kaplun
  • 38.5k
  • 15
  • 102
  • 113

Context bounds desugar into implicit parameters, e.g. consider a function that leverages the Monoid type class:

def suml[T: Monoid](xs: List[T]) = { val T = implicitly[Monoid[T]] xs.foldLeft(T.mzero)(T.mplus) } 

where the : Monoid part is a context bound, gets translated to:

def suml[T](xs: List[T])(implicit evidence$1: Monoid[T]]) = { ... } 

therefore the following compiles, too:

def suml[T: Monoid](xs: List[T]) = { val T = evidence$1 ... } 

(note that the name T in val T is chosen arbitrarily to match the type T but bears no lexical association with type T)