When to use duck typing

Overusing duck typing can negatively affect code quality and application performance. You should not avoid creating common interfaces in favor of duck typing. It should be really only used in cases when we cannot implement a common interface between different types. The argument about limiting the use of duck typing is further enhanced by the fact that, under the hood, they use reflection, which is slower and negatively impacts performance.

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.