开发者

What are the alternatives to subtype polymorphism in scala?

I'm interested to know the complete set of alternatives to subtyp开发者_如何学运维e polymorphism in scala.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜