Duck typing

A significant part of the work of a developer is to minimize the amount of code duplication. There are multiple different approaches to do this, including inheritance, abstraction, generics, type classes, and so on. There are cases, however, where strongly typed languages will require some extra work in order to minimize some of the duplication. Let's imagine that we have a method that can read and print the contents of a file. If we have two different libraries that allow us to read a file, in order to use our method, we will have to make sure the methods that read the file somehow become the same type. One way would be by wrapping them in a class that implements a specific interface. Provided that in both the libraries the read ...

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.