简体   繁体   English

scala中子类型多态性有哪些替代方法?

[英]What are the alternatives to subtype polymorphism in scala?

我很想知道scala中子类型多态性的完整替代方法。

The basic tools for of achieving statically-checkable polymorphism in Scala are 在Scala中实现静态可检查多态性的基本工具是

  • Subtyping (bog-standard OO polymorphism) 子类型(沼泽标准OO多态性)
  • Type parameterization (allows polymorphic variance and constraints) 类型参数化(允许多态方差和约束)
  • Self-typing (allows restriction and specialization of OO polymorphism) 自我分型(允许OO多态性的限制和专业化)
  • 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. 值得注意的是Scala多态性中for-comprehension的位置。 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. 在表面之下,for-comprehensions只是调用filter / map / flatMap的语法糖,并且这些方法的实现往往是高度多态的。 Thus, what look to be comparatively simple for-loops can result in some really impressive polymorphic effects. 因此,看起来相对简单的for循环可能会产生一些非常令人印象深刻的多态效应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM