why Functor has to be higher kinded type
Functor
has to be higher kinded because we want to abstract over a type parameter which itself takes a type parameter (we call this a type constructor, think Functor[List]
as a concrete example).
The type of types that Functor
deals with are called "first order kinds", their kind is of * -> *
. When you looking at concrete instances for Functor
, you see we dont provide the inner type parameter. For example, when you define a functor for List
as you did in your example, you define it as a Functor[List]
. We're not creating a functor for a proper type (i.e. List[Int]
), but rather any type contained inside the List
. This abstraction brings great power, because you can utilize it for any proper List
type (of kind *), be it List[String]
, List[Int]
, etc..
I always like to refer to the image drawn by Adriaan Moore in his paper "Genrics Of A Higher Kind":
What does the type parameter F really mean
F
s sole purpose is to define a contract with the implementer of the Functor
. By the signature of F
we can deduce what kind of type Functor
expects. When we see that it has one "placeholder" ([_]
) we know, by convention, that this means that F
should take a single type parameter. If we think about all the types that take a single type parameter, we can see that there are many, for example List
, Option
, Try
, Future
, Task
, etc.
For a more broad explanation regarding higher kinded types, see What is a higher kinded type in Scala?
Functor[F[_]]
. It looks to me that functor is aiming to transform F[A] to F[B] given A=>B, F's meaning here doesn't really matter. It can be a container or context or some others, as long as F[A] and F[B] is meaningful. – Tommap
look like? – sepp2kFunctor[F[_]]
looks like that because we want to useFunctor[List]
andFunctor[Option]
andFunctor[Stream]
and others like that.List
,Option
andStream
are all higher-kinded types that have exactly one type parameter.F[_]
is a way to express this last fact._
is a placeholder for the type parameter ofF
. – n. 1.8e9-where's-my-share m.