/
higher-kind.js
65 lines (49 loc) · 2.05 KB
/
higher-kind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Skb from 'components/skb-page'
export const id = "higher-kind"
export const title = "Scala higher kind"
export const date = "2020-12-09T17:00:01"
const scastieId = "fC18HjQmT6Gbh5mTox5zbw"
const mainInfoBox = <>
<p>
Let's dive more into object oriented programming ideas but with a dash of functional programming.
</p>
<p>
The idea today is to go a bit further with <code>trait</code> and be able to describe behaviors of <i>higher kinds</i>. It is just a fancy terms to describe type that accept type as parameter, such as <code>List[A]</code>, the <code>A</code> being the type accepted as parameter.
</p>
</>
const detailedInfoBox = <>
<p>
To create a generic trait that “target” type that take type(s) as parameter, the syntax is:
</p>
<pre>trait MyTrait[F[_]] { }
</pre>
<p>
The <code>F[_]</code> means that we are expecting a type <code>F</code> that take one type parameter as argument, the <code>_</code>.
</p>
<p>
Note that it is possible to target a type that takes two types as input with the following syntax: <code>F[_, _]</code>. You can imagine that it is possible to target type with more than two following the same logic.
</p>
<p>
When you use a parameterized type you do something like <code>List[Int]</code> so if you want to use the type itself you simply has to do <code>Trait[List]</code>, not need to add anything after the <code>List</code>.
</p>
<p>
You also noticed that you can create your own type and it will behave the same way. Look at <code>CounterT</code> in our example. It takes one type as parameter so it can be used with our <code>trait</code>.
</p>
</>
const description = "Scala Knowledge Bits - Scala higher kind - Periodic exercise to learn bits of knowledge about Scala. Scala higher kind here."
const prevUrl = "constraint-inheritance"
const nextUrl = "_-wildcard"
export const pageData = {
id,
title,
date,
scastieId,
mainInfoBox,
detailedInfoBox,
description,
prevUrl,
nextUrl
}
export default function Page() {
return <Skb lesson={pageData} />
}