Skip to content

Commit

Permalink
Merge pull request #47 from twitter/lahosken_typetype
Browse files Browse the repository at this point in the history
Discuss motive behind higher-kinded types example before showing the magic
  • Loading branch information
mariusae committed Sep 4, 2012
2 parents 784da19 + 2408c39 commit a3d2ea0
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions web/_posts/2011-05-06-lesson.textile
Expand Up @@ -143,13 +143,9 @@ Combined, these often result in less code, especially when threading through vie


h3. Higher-kinded types & ad-hoc polymorphism h3. Higher-kinded types & ad-hoc polymorphism


Scala can abstract over "higher kinded" types. This is analagous to function currying. For example, whereas "unary types" have constructors like this: Scala can abstract over "higher kinded" types. For example, suppose that you needed to use several types of containers for several types of data. You might define a <code>Container</code> interface that might be implemented by means of several container types: an <code>Option</code>, a <code>List</code>, etc. You want to define an interface for using values in these containers without nailing down the values' type.


<pre> This is analagous to function currying. For example, whereas "unary types" have constructors like <code>List[A]</code>, meaning we have to satisfy one "level" of type variables in order to produce a concrete types (just like an uncurried function needs to be supplied by only one argument list to be invoked), a higher-kinded type needs more.
List[A]
</pre>

Meaning we have to satisfy one "level" of type variables in order to produce a concrete types (just like an uncurried function needs to be supplied by only one argument list to be invoked), a higher-kinded type needs more:


<pre> <pre>
scala> trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A } scala> trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A }
Expand All @@ -160,7 +156,7 @@ res23: java.lang.Object with Container[List] = $anon$1@7c8e3f75
scala> res23.put("hey") scala> res23.put("hey")
res24: List[java.lang.String] = List(hey) res24: List[java.lang.String] = List(hey)


scala> res23.put(123) scala> res23.put(123)
res25: List[Int] = List(123) res25: List[Int] = List(123)
</pre> </pre>


Expand Down Expand Up @@ -212,7 +208,7 @@ class MyContainer extends Container {


fails to compile, since we are specifying Ordered for *Container*, not the particular subtype. fails to compile, since we are specifying Ordered for *Container*, not the particular subtype.


To reconcile this, we use F-bounded polymorphism. To reconcile this, we instead use F-bounded polymorphism.


<pre> <pre>
trait Container[A <: Container[A]] extends Ordered[A] trait Container[A <: Container[A]] extends Ordered[A]
Expand Down

0 comments on commit a3d2ea0

Please sign in to comment.