Richard Searle's Blog

Thoughts about software

Defaulting type with Scala implicits

Posted by eggsearle on July 13, 2013

This stackoverflow answer shows how a default type can be specified, using some interesting side effects of the Scala implicit rules.

Scala 2.10.1 allows the code to be further simplified

object DefaultsTo {
 implicit def default[B] = new DefaultsTo[B, B]
 implicit def overrideDefault[A,B] = new DefaultsTo[A,B]
}

The  T DefaultsTo Node looks rather strange but is actually the infix form of DefaultsTo[T,Node]

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: