Replies: 2 comments 2 replies
-
I found this thread too, but I didn't really get my answers. Can someone give me an example in which my snippet code above does not cover? |
Beta Was this translation helpful? Give feedback.
-
Ok my good fellas, if questions 2 and 3 are your questions too, it means you haven't understood HKTs like me. I'm going to explain why, but please please let me know if I'm wrong again 😂:
It's called Higher-Kinded Type because if Kinds are like
Why this is important? Think about Functor. Functor works on Option and Either.
Implementing const optionFunctor: Functor1<'Option'> = {
// map: <A,B>(f: A=>B) => Option<A> => Option<B>
map: <A,B>(f: A=>B) => Kind<'Option',A> => Kind<'Option', B> = ...
} What about const eitherFunctor: Functor2<'Either'> = {
// map: <E,A>(f: A=>B) => Either<E,A> => Option<E,A>
map: <E,A>(f: A=>B) => Kind<'Either',E,A> => Kind<'Either',E,A> = ...
} How can we abstract Functor1, Functor2, ... and have only one Functor definition? By introducing HKT (Higher-Kinded Type) interface Functor<F> {
map: <A,B>(f: A=>B) => HKT<F,A> => HKT<F,B>
} In another words, no matter how many type parameters our type constructor receives (Option is 1 and Either is 2, ...) Functor only changes one Type parameter (first one) and keep the rest fixed, or in another word it works over HKT. How about BiFunctor that its map function changes 2 type parameters? Then our abstraction should at least be over the first 2 type parameters which are HKT2. Now you can implement cool functions over Higher-kinded Types and not only a specific kind like |
Beta Was this translation helpful? Give feedback.
-
I was looking into how Higher-Kinded Types become possible in fp-ts. Amazing stuff. I have some questions:
1- Why Kind is using
extends
in both type parameter and it's definition?For Kind in module
HTK.ts
I'm seeing we have:Doesn't the extends in the type parameter enough? I mean why not:
2- Why do we need
HKT
definition, isn'tKind
enough?Basically what scenario the code below is not supporting?
3- Why do we need
URI
in the Type Class instances?I mean where do we find need to differentiate between Type Class instances, that we want to have URI in them?
Beta Was this translation helpful? Give feedback.
All reactions