Definition of PolyN#at
at
is a general way to work with Poly
.
~>
with apply
is a special case of Poly1
. apply
here is used to define implicit method using at
:
implicit def caseUniv[T] = at[F[T]](apply(_))
Method at
is defined in PolyN
(for instance in Poly1
) like this:
trait PolyN extends Poly { outer =>
type Case[T1, T2, ..., TN] = poly.Case[this.type, T1 :: T2 :: ... :: TN :: HNil]
object Case {
type Aux[T1, T2, ..., TN, Result0] = poly.Case[outer.type, T1 :: T2 :: ... :: TN :: HNil] { type Result = Result0 }
}
class CaseBuilder[T1, T2, ..., TN] {
def apply[Res](fn: (T1, T2, ..., TN) => Res) = new Case[T1, T2, ..., TN] {
type Result = Res
val value = (l: T1 :: T2 :: ... :: TN :: HNil) => l match {
case a1 :: a2 :: ... :: aN :: HNil => fn(a1, a2, ..., aN)
}
}
}
def at[T1, T2, ..., TN] = new CaseBuilder[T1, T2, ..., TN]
}
In case of Poly1
:
trait Poly1 extends Poly { outer =>
type Case[T1] = poly.Case[this.type, T1 :: HNil]
object Case {
type Aux[T1, Result0] = poly.Case[outer.type, T1 :: HNil] { type Result = Result0 }
}
class CaseBuilder[T1] {
def apply[Res](fn: (T1) => Res) = new Case[T1] {
type Result = Res
val value = (l: T1) => l match {
case a1 :: HNil => fn(a1)
}
}
}
def at[T1] = new CaseBuilder[T1]
}
So at[Int]
creates an instance of CaseBuilder[Int]
and at[Int].apply[String](_.toString)
or just at[Int](_.toString)
(synax sugar for apply
method call) creates an instance of poly.Case[this.type, Int :: HNil]{ type Result = String }
.
So with implicit def iterable[T, L[T] <: Iterable[T]] = at[L[T]](_.iterator)
you create an implicit method to create a poly.Case[this.type, L[T] :: HNil]{ type Result = Iterator[T] }
.
This implicit method is used in map
(and in some other polymorphic functions).
Implementation of HList#map
map
is defined like this:
def map(f : Poly)(implicit mapper : Mapper[f.type, L]) : mapper.Out = mapper(l)
(L
is the type of HList
)
To create a Mapper
compiler looks for Case1[Fn, T]
.
For map(f)
on A :: B :: ... :: HNil
compiler have to find implicits for Case1[f.type, A]
, Case1[f.type, B]
and so on.
In case of List[Int] :: HNil
the only implicit Case1
needed is Case1[f.type, List[Int]]
.
Note that Case1
is defined like this:
type Case1[Fn, T] = Case[Fn, T :: HNil]
So we have to find an implicit value for Case[f.type, List[Int] :: HNil]
.
In case f
is an object
one of the places to search for implicits is f
fields and methods. So compiler will find Case
defined in f
.