Using pimp my library

The pimp my library design pattern is really easy to use. Let's see an example in which we want to add some useful methods to the standard String class. Of course, we cannot modify its code, so we need to do something else:

package object pimp {  implicit class StringExtensions(val s: String) extends AnyVal {    def isAllUpperCase: Boolean =      !(0 until s.length).exists {        case index =>          s.charAt(index).isLower      }  }}

In the preceding code, we have a package object. It gives us the convenience to not do anything extra in order to be able to access its members from the classes in the same package in Scala. It can be a simple object, but then we will have to import ObjectName._ in order to gain access to the members.

The preceding ...

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.