-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.xml
More file actions
175 lines (119 loc) · 7.51 KB
/
index.xml
File metadata and controls
175 lines (119 loc) · 7.51 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>João Da Silva</title>
<link>https://jsilva.github.io/</link>
<description>Recent content on João Da Silva</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<lastBuildDate>Mon, 19 Sep 2016 18:16:24 +0200</lastBuildDate>
<atom:link href="https://jsilva.github.io/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Akka notes</title>
<link>https://jsilva.github.io/blog/akka-notes/</link>
<pubDate>Mon, 19 Sep 2016 18:16:24 +0200</pubDate>
<guid>https://jsilva.github.io/blog/akka-notes/</guid>
<description>
<h1 id="akka-notes">Akka notes</h1>
</description>
</item>
<item>
<title>About Me</title>
<link>https://jsilva.github.io/about/</link>
<pubDate>Thu, 15 Sep 2016 18:14:22 +0200</pubDate>
<guid>https://jsilva.github.io/about/</guid>
<description>
<h1 id="about-me">About Me</h1>
<p>Backend Software Developer and Team Lead.</p>
<p>I have wide experience in frontend, backend and devops development for more than seven years in middle and long-term projects. My primary expertise includes a number of industry-proof technologies around the Scala ecosystem and the Lightbend Reactive platform such as Akka, Playframework, Apache Spark.</p>
<p>I also have good skills in distributed systems development, computer vision, natural language processing, graph databases and Ansible for systems provision.</p>
<p>Looking for remote software development contract work. I can provide you with my personal services, or as a Lead of the team of my assistants from Czech Republic.</p>
<p><a href="https://jsilva.github.io/contact">Hire me</a></p>
</description>
</item>
<item>
<title>Projects</title>
<link>https://jsilva.github.io/works/</link>
<pubDate>Thu, 15 Sep 2016 18:14:22 +0200</pubDate>
<guid>https://jsilva.github.io/works/</guid>
<description></description>
</item>
<item>
<title>Scala Type covariance and contravariance</title>
<link>https://jsilva.github.io/blog/scala-type-covariance-and-contravariance/</link>
<pubDate>Tue, 19 Jul 2016 18:16:24 +0200</pubDate>
<guid>https://jsilva.github.io/blog/scala-type-covariance-and-contravariance/</guid>
<description>
<h2 id="type-covariance-and-contravariance">Type Covariance and Contravariance</h2>
<p>Quoting from <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29">wikipedia</a></p>
<blockquote>
<p>Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return types).</p>
</blockquote>
<ul>
<li>covariant: converting from wider (double) to narrower (float).</li>
<li>contravariant: converting from narrower (float) to wider (double).</li>
<li>invariant: Not able to convert.</li>
</ul>
<p>The ability to send a collection of subclass instances to a collection of base class is called covariance</p>
<p>The ability to send a collection of superclass instances to a collection of subclass is called contravariance</p>
<p>By default scala does not allow covariance and contravariance, but there are some good use cases where you might want to use a derived type as a super type.</p>
<p>Lets prepare some base code:</p>
<pre><code>class Person(val name: String) {
override def toString = name
}
class Employee(override val name: String) extends Person(name)
val empList = List(new Employee(&quot;Joao&quot;), new Employee(&quot;Andre&quot;))
val peopleList = List(new Person(&quot;Martin&quot;), new Person(&quot;Jonas&quot;))
def sayHi(people:List[Employee]) = people.map { println _ }
sayHi(empList)
sayHi(peopleList) // compilation ERROR
</code></pre>
<p>The error is pretty obvious we can’t send a List[Person] as the sayHi function expects a List[Employee] as it’s formal parameter, and here is where covariancecomes into place.</p>
<h3 id="covariance">Covariance</h3>
<p>To solve the above compilation error we have to let scala compiler know that it’s ok to treat a Person as an Employee, we
do that as follows:</p>
<pre><code>def sayHi[T &lt;: Person](people: List[T]) = people.map { println _ }
</code></pre>
<p>sayHi has now a special syntax [T &lt;: Person]. This syntax indicates that T sould be of Type Person or any class that derives from Person, which means that know we can have something like:</p>
<pre><code>sayHi(empList) sayHi(peopleList)
</code></pre>
<p>You can also indicate covariance at the class level by making the parameterized type+T instead off T.</p>
<pre><code>class SomeCollection[+T]
var lst1 = new SomeCollection[Employee]
var lst2: SomeCollection[Person] = lst1
</code></pre>
<p>The above snippet is possible since Employee is a subclass of Person</p>
<h3 id="contravariance">Contravariance</h3>
<p>Contravariance is the literally the opposite of Covariance, Lets imagine we want to add Employee to Person, as you can see the relationship is from subclass to superclass.</p>
<pre><code>def appendToPerson[T, D &lt;: T](employees: List[T], people: List[D]) = people ++ employees
</code></pre>
<p>With the syntax [T, D &lt;: T], we have constrained the parameterized type D to be of the type T or a super type of T, so basically T sets the lower level in the Type hierarchy for type D. In our example the type D (Person) can be of type T (Employee) or its superclass.</p>
<p>Scala also has class level support for setting contravariance and as you might guess the parameterized type is -T</p>
<h3 id="summary">Summary</h3>
<ul>
<li>A class C[T], when a method accepts only C[T] is invariant</li>
<li>A class C[+T], when a method accepts C[T] i can also get any of it’s dervatives</li>
<li>A class C[+-T], when a method accepts C[T] i can also get any of it’s super classes</li>
</ul>
<p>Function Type bounds:</p>
<pre><code>def fun[T &lt;: A](arg:T) // covariant, arg must be of type T or any of it's subclasses
def fun[T &gt;: A](arg:T) // Contravariant, arg must be of type T or any of it's super class
</code></pre>
</description>
</item>
<item>
<title>Contact</title>
<link>https://jsilva.github.io/contact/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://jsilva.github.io/contact/</guid>
<description></description>
</item>
<item>
<title>What I Can Do</title>
<link>https://jsilva.github.io/skills/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://jsilva.github.io/skills/</guid>
<description></description>
</item>
</channel>
</rss>