The Artima Developer Community
Sponsored Link

Java Buzz Forum
Scala: Types of a higher kind

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Mathias Bogaert

Posts: 618
Nickname: pathos
Registered: Aug, 2003

Mathias Bogaert is a senior software architect at Intrasoft mainly doing projects for the EC.
Scala: Types of a higher kind Posted: Sep 6, 2013 7:26 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Mathias Bogaert.
Original Post: Scala: Types of a higher kind
Feed Title: Scuttlebutt
Feed URL: http://feeds.feedburner.com/AtlassianDeveloperBlog
Feed Description: tech gossip by mathias
Latest Java Buzz Posts
Latest Java Buzz Posts by Mathias Bogaert
Latest Posts From Scuttlebutt

Advertisement
One of the more powerful features Scala has is the ability to generically abstract across things that take type parameters. This feature is known as Higher Kinded Types (HKT). This feature allows us to write a library that works with a much wider array of classes, whereas without the feature you are condemned to bespoke and error ridden code duplication for each class that may want the functionality. Type constructors Essentially what HKT gives us is the ability to generalize across type constructors – where a type constructor is anything that has a type parameter. For instance List[_] is not a type, the underscore is a hole into which another type may be plugged, constructing a complete type. List[String] and List[Int] being examples of complete (or distinct) types. Kinds Now that we have a type constructor we can think of several different kinds of them, classified by how many type parameters they take. The simplest – like List[_] – that take a single param have the kind: (* -> *) This says: given one type, produce another. For instance, given String produce the type List[String]. Something that takes two parameters, say Map[_, _], or Function1[_, _] has the kind: (* -> * -> *) This says: given one type, then another, produce the final type. For instance given the key type Int and the value type String produce the type Map[Int, String]. Furthermore, you can have kinds that are themselves parameterized by higher kinded types. So, something could not only take a type, but take something that itself takes type parameters. An example would be the covariant functor: Functor[F[_]], it has the kind: ((* -> *) -> *) This says: given a simple higher kinded type, produce the final type. For instance given a type constructor like List produce the final type Functor[List]. Utility Say we have some standard pattern for our data-structures where we want to be able to consistently apply an operation of the same shape. Functors are a nice example, the covariant functor allows us to take a box holding things of type A, and a function of A => B and get back a box holding things of type B. In Java, there is no way to specify that these things share a common interface, or that we simply want transformable boxes. We need to either make this static eg. Guava’s Lists and Iterables, or bespoke on the interface, eg: fugue’s Option or atlassian-util-concurrent’s Promise. There is simply no way to unify these methods on either some super interface or to specify that you have – or require – a “mappable/transformable” box. With HKT I can represent the covariant functor described above as: 123456789101112131415161718192021222324252627282930313233trait Functor[F[_]] {   def map[A, B](fa: F[A])(f: A => B): F[B] }   // implement for java's List import java.util.{ List => JList } implicit object JavaListFunctor extends Functor[JList] {   import collection.JavaConverters._     def map[A, B](fa: JList[A])(f: A => B): JList[B] =     (for (a <- fa.asScala) yield f(a)).asJava […]

Read: Scala: Types of a higher kind

Topic: A clean approach to Wicket models Previous Topic   Next Topic Topic: Reasons to consider spring-boot for your next Spring based application!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use