Skip to content

Instantly share code, notes, and snippets.

@manjuraj
Last active August 29, 2015 14:05
Show Gist options
  • Save manjuraj/596f38e337d64e40d247 to your computer and use it in GitHub Desktop.
Save manjuraj/596f38e337d64e40d247 to your computer and use it in GitHub Desktop.
Typeclasses in the wild
// https://github.com/twitter/util/blob/master/util-app/src/main/scala/com/twitter/app/Flag.scala
// Type class / Typeclass / Type traits
// References
// - http://www.haskell.org/haskellwiki/Typeclassopedia
// - http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf
// - https://github.com/channingwalton/typeclassopedia
// - http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf
// - http://debasishg.blogspot.com/2010/06/scala-implicits-type-classes-here-i.html
// - http://www.casualmiracles.com/2012/01/08/a-small-functor-example/
// - http://www.casualmiracles.com/2012/05/03/a-small-example-of-the-typeclass-pattern-in-scala/
// - High Wizardry in the Land of Scala: http://vimeo.com/28793245
// Type classes are simlar to java interfaces. Both define a set of types/classes
// which implmenet a specified set of operations. However, there are a couple of
// important ways in which type classes are more general than java interfaces
//
// - When a java class is defined, any interfaces it implements must be declared.
// Type class instance, on the other hand, are declared separately from the
// declaration of the corresponding type, and can even be put in a separate
// module
// - The types that can be specified for type class methods are more general and
// flexible than the signatures that can be given for java interface methods,
// especially when multi-parameter type classes enter the picture
// We refer to operations of type class as "type class" polymorphic methods
// Scala enables the typeclass pattern using traits and implicits, and whilst
// Scala’s implementation is more verbose than Haskell’s, it comes with greater
// flexibility. Haskell only allows a single typeclass instance globally,
// whereas Scala allows any number to be available.
// What do we gain with typeclasses?
// Instances are declared outside the types themselves. Int knows nothing about Show
// - open world assumption
// - retroactive extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment