Skip to content

Instantly share code, notes, and snippets.

eggm0n /
Created May 11, 2018
Higher kinded types notes

F is a higher kinded type/function at the type level/type level constructor. Examples are Future, Option, List, etc.

List is a type constructor and Int is a proper type. If I apply Int to List then I get List[Int]which is a proper type. So far, all we have done is used a generic higher kinded type (F) instead of a concrete one (Future).

Typeclasses are a form of ad-hoc polymorphism. They allow you to an enrich an existing type with new capabilities. For example, let’s say I wanted to add two Orders together. The source code for an Order isn’t in our control but we would like to extend it.

def add[A](x: A, y: A)(implicit a: Addable[A]): A = a.add(x, y)

This is an example of a typeclass method. Notice that in order to use the addmethod, your A’s need to have a typeclass implementation of Addable[A].

eggm0n / example.scala
Created Nov 30, 2017
covariance and contravariance
View example.scala
class GParent
class Parent extends GParent
class Child extends Parent
class Box[+A]
class Box2[-A]
def foo(x : Box[Parent]) : Box[Parent] = identity(x)
def bar(x : Box2[Parent]) : Box2[Parent] = identity(x)
foo(new Box[Child]) // success
foo(new Box[GParent]) // type error
bar(new Box2[Child]) // type error
View notes
> git init
A folder used for storing multiple files.
A directory where Git has been initialized to start version controlling your files.
> git status
The .git directory
eggm0n / validate.scala
Last active Sep 25, 2017
extra validation on play json reads
View validate.scala
import play.api.libs.json._
import scala.reflect.runtime.universe._
def checkedReads[T](underlyingReads: Reads[T])(implicit typeTag: TypeTag[T]): Reads[T] = new Reads[T] {
def classFields[U: TypeTag]: Set[String] = typeOf[U].members.collect {
case m: MethodSymbol if m.isCaseAccessor =>
def reads(json: JsValue): JsResult[T] = {

Keybase proof

I hereby claim:

  • I am eggm0n on github.
  • I am eggm0n ( on keybase.
  • I have a public key ASBGbG6y1gyou649rBsxEYfgV-4tyWRU7fg789QJRnKfLgo

To claim this, I am signing this object: