Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the alternatives to subtype polymorphism in scala?

I'm interested to know the complete set of alternatives to subtype polymorphism in scala.

like image 787
Channing Walton Avatar asked Aug 29 '10 19:08

Channing Walton


1 Answers

The basic tools for of achieving statically-checkable polymorphism in Scala are

  • Subtyping (bog-standard OO polymorphism)
  • Type parameterization (allows polymorphic variance and constraints)
  • Self-typing (allows restriction and specialization of OO polymorphism)
  • Implicit conversion (allows post-facto polymorphic construction)
  • Structural typing (allows polymorphism based on features, rather than type)
  • General type bounds (allowing extremely precise constraints on allowed polymorphism)
  • Pattern matching (allows polymorphism based on data structure, similar to abstract data types)
  • Higher-kinded types (allowing polymorphism over polymorphic constructions)

Calling all of these 'alternatives' is probably the wrong word, since they are so well integrated. It's not uncommon for some polymorphic algorithms to be expressed by using of several of these tools in conjunction.

It's also worth noting the place of for-comprehensions in Scala polymorphism. For comprehensions don't seem particularly polymorphic. Below the surface, for-comprehensions are just syntactic sugar for calls to filter/map/flatMap, and implementations of those methods tend to be highly polymorphic. Thus, what look to be comparatively simple for-loops can result in some really impressive polymorphic effects.

like image 198
Dave Griffith Avatar answered Sep 25 '22 07:09

Dave Griffith