O'Reilly logo

Scala Puzzlers by Nermin Šerifovic, Andrew Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Puzzler 29

Implicit Kryptonite

Scala's implicits provide a flexible mechanism to access context-specific values and behavior. By changing the implicits in scope, you can switch contexts in different parts of your application easily.

This code example models a simple baggage scanner that operates in two modes, or contexts: normal operation and a special "test mode." In normal operation, the scanner's console indicates which type of item is being scanned and the alarm button is "live," triggering the alarm when activated.

To ensure that the operator keeps paying attention, the scanner also has a test mode that is activated at random intervals. In test mode, the console ignores the actual item inside the scanner and pretends to have found a dangerous ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required