Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ title: Main
description: > # this means to ignore newlines until "baseurl:"
Programmer and programming related content.
baseurl: "" # the subpath of your site, e.g. /blog
url: "http://pgebal.github.io" # the base hostname & protocol for your site, e.g. http://example.com
url: "http://pawelgebal.com"
twitter_username: pawelwgebal
github_username: pgebal
disqus:
Expand Down
60 changes: 60 additions & 0 deletions _posts/2016-12-04-covariant_contravariant.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
layout: post
title: "A short one: Covariant type in a contravariant position"
date: 2016-12-04 12:00:00 +0100
categories: scala update
tags: scala functional-programming
excerpt: It will never happen to you after reading this.
---
There is an exercise in the great [Functional Programming in Scala][functional-programming-in-scala-affiliate-link] book, that requires you to
implement **Option** trait and it's **getOrElse** method.
My first try on it was:
{: .text-justify}
{% highlight scala %}
sealed trait Option[+A] {

def getOrElse(default: => A): A = this match {
case Some(a) => a
case None => default
}
}

case class Some[+A](value: A) extends Option[A]
case object None extends Option[Nothing]
{% endhighlight %}

IntelliJ Idea was quick to tell me that it's not a right solution because:
{% highlight scala %}
Covariant type A in contravariant position in type A of value default.
{% endhighlight %}

I looked to the book and noticed that they suggest to use the following definition for **getOrElse**:
{% highlight scala %}
def getOrElse[B >: A](default: => B): B = this match {
case Some(a) => a
case None => default
}
{% endhighlight %}

At first I didn't know why the solution required to use type parameter **B** that is a supertype of **A**.
It turned out to be quite simple.
Notice that **A** is a covariant type parameter for **Option**.
That means for every **A** that is a subtype of **B**, **Option[A]** is a subtype of **Option[B]**.
So whenever we use expression of type **Option[A]**, we have to be able to substitute it with expression of type **Option[B]**.
Study this:
{: .text-justify}

{% highlight scala %}
class B
class A extends B

val b: Option[B] = None
val a: Option[A] = None

val default: B = b.getOrElse(new B)
{% endhighlight %}
Note that in the last line we could use **a** instead of **b** and the code would stay valid. That's thanks to <nobr><b>[B >: A]</b></nobr> bound in **getOrElse** method definition. If it wasn't for that **a.getOrElse** would not accept an argument of type **B**. Keep this example in mind and embrace [Liskov substitution principle][liskov-substitution-principle] when implementing your own classes with type parameters.
{: .text-justify}

[functional-programming-in-scala-affiliate-link]: https://www.amazon.com/gp/product/1617290653/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290653&linkCode=as2&tag=pawelgebal-20&linkId=f3ec949cadbc0ff936a5aae1dcc51c0a
[liskov-substitution-principle]:https://en.wikipedia.org/wiki/Liskov_substitution_principle
4 changes: 4 additions & 0 deletions _site/about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@



<a class="page-link" href="/cookies/">Cookies</a>






Expand Down
6 changes: 3 additions & 3 deletions _site/feed.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xml" href="http://localhost:4000/feed.xslt.xml"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.3.1">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2016-11-27T13:03:30+01:00</updated><id>http://localhost:4000//</id><title type="html">Main</title><subtitle>Programmer and programming related content.
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xml" href="http://localhost:4000/feed.xslt.xml"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.3.1">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2016-12-02T22:07:19+01:00</updated><id>http://localhost:4000//</id><title type="html">Main</title><subtitle>Programmer and programming related content.
</subtitle><entry><title type="html">Type class pattern</title><link href="http://localhost:4000/scala/update/2016/11/27/type-classes.html" rel="alternate" type="text/html" title="Type class pattern" /><published>2016-11-27T11:00:00+01:00</published><updated>2016-11-27T11:00:00+01:00</updated><id>http://localhost:4000/scala/update/2016/11/27/type-classes</id><content type="html" xml:base="http://localhost:4000/scala/update/2016/11/27/type-classes.html">&lt;p&gt;To get the most out of this article I encourage you to put the pieces of code you find here into a scala worksheet and play with it.&lt;/p&gt;

&lt;p&gt;Let’s have a class that we want to serialize to JSON:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If we would go Java style, we might have come up with this:&lt;/p&gt;
&lt;p&gt;If we went Java style, we might come up with this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;

Expand Down Expand Up @@ -47,7 +47,7 @@ How about sticking to the Single Responsibility Principle and pushing serializat
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ColorJsonSerializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toJson&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yellow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p class=&quot;text-justify&quot;&gt;to get a JSON representation of yellow.
That’s better then the previous solution, but we can pimp it using some implicit magic.
That’s better than the previous solution, but we can pimp it using some implicit magic.
First let’s make &lt;strong&gt;JsonSerializer.toJson&lt;/strong&gt; method look like it’s a method of &lt;strong&gt;Color&lt;/strong&gt; class.
To achieve that, let’s add an implicit wrapper over &lt;strong&gt;Color&lt;/strong&gt;.&lt;/p&gt;

Expand Down
4 changes: 4 additions & 0 deletions _site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@



<a class="page-link" href="/cookies/">Cookies</a>






Expand Down
4 changes: 4 additions & 0 deletions _site/scala/update/2016/11/20/method-function-eta.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@



<a class="page-link" href="/cookies/">Cookies</a>






Expand Down
8 changes: 8 additions & 0 deletions cookies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
layout: page
title: Cookies
permalink: /cookies/
---
This blog uses cookies to track site usage by Google Analytics.

By browsing this site, you consent to the use of cookies.