-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
constraint-inheritance.js
88 lines (67 loc) · 2.57 KB
/
constraint-inheritance.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Skb from 'components/skb-page'
export const id = "constraint-inheritance"
export const title = "Scala constraint inheritance"
export const date = "2020-12-07T17:01:22"
const scastieId = "1rovwDJFTRaHBS7e2c2ofg"
const mainInfoBox = <>
<p>
More inheritance today, more specifically how to get help from the compiler in complex inheritance patterns
</p>
<p>
The example here is simple to illustrate the feature but imagine what it would look like in a much more complex project.
</p>
</>
const detailedInfoBox = <>
<p>
We saw multiple inheritance last time, but it can quickly become messy if a lot of your <code>trait</code> are depending on each other.
</p>
<p>
It would be great if we could ask the compiler to remind us that two or more trait needs to always be together for the code to work well.
</p>
<p>
Well, guess what? It is !
</p>
<p>
It is possible in Scala to create a <code>trait</code> that cannot be used if not mixed in with a selection of other <code>trait</code>. Let's look at the syntax:
</p>
<pre>
<p>trait MyTrait { name: DependencyTrait => </p>
<p> // content of trait</p>
<p>}</p>
</pre>
<p>
The <code>name</code> can be any variable name if you need to disambiguates two or more trait that might have same function names. It can also be <code>this</code> if you do not need an extra handler for it.
</p>
<p>
And <code>DependencyTrait</code> can be one <code>trait</code> that is required to create <code>MyTrait</code> but it can more than one if you replace it by <code>trait1 with trait2 with trait3 ...</code>.
</p>
<p>
In case of failure to comply those constraint, the compiler will give you the following error:
</p>
<pre>illegal inheritance;
self-type [Class you are working on] does not conform to [MyTrait]'s selftype [MyTrait] with [DependencyTrait]
</pre>
<p>
You should have seen this error message in the exercise and you now know how to fix it.
</p>
<p>
You also now know how to befriend the compiler and making sure you don't forget a critical <code>trait</code> in the mix for your classes.
</p>
</>
const description = "Scala Knowledge Bits - Scala constraint inheritance - Periodic exercise to learn bits of knowledge about Scala. Scala constraint inheritance here."
const prevUrl = "multiple-inheritance"
const nextUrl = "higher-kind"
export const pageData = {
id,
title,
date,
scastieId,
mainInfoBox,
detailedInfoBox,
description,
prevUrl,
nextUrl
}
export default function Page() {
return <Skb lesson={pageData} />
}