A Pitfall in Scala Implicits

by Ian Macalinao

May 21, 2017

Consider the following code:

For those unfamiliar with Cats/Scalaz, Monoid is a simulacrum-generated typeclass, so its apply method simply resolves the implicit in scope. It looks something like this:

This code will not compile because the Monoid cannot be implicitly resolved. However:

This will compile and print null. It resolves the implicit using the default empty null value!

Be careful when exporting implicits in your objects.