Implicits	Inspected	and
Explained
ScalaDays	2016	Berlin
Tim	Soethout	-	ING	Bank
http://blog.timmybankers.nl
Implicits	Inspected	and
Explained
ScalaDays	2016	Berlin
Tim	Soethout	-	ING	Bank
Outline:	Introduction	-	Implicits	-	Resolving	-	Type	Classes	-	Wrap	up
http://blog.timmybankers.nl
About	myself
Tim	Soethout
Functional	programmer	at	heart
Scala/FP	evangelist/trainer	inside	ING	Bank
PhD	Candidate
What?
Use	values	without	explicit	reference
OO:	is	a	+	has	a
Implicits	add:	is	viewable	as	a
Loose	Coupling,	Tight	Cohesion
Examples
Akka
Futures
trait ScalaActorRef
def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit = ...
}
actorRef ! SomeMessage("text")
object Future {
def apply[T](body: =>T)(implicit executor: ExecutionContext): Future[T] = ...
}
Future {
doExpensiveComputation()
}
Examples	(2)
Collections
trait TraversableOnce[+A] {
def sum[B >: A](implicit num: Numeric[B]): B = ...
}
List(1,2,3).sum
res0: Int = 6
Examples	(3)
Finagle
@implicitNotFound("Builder is not fully configured: Cluster: ${HasCluster}, Codec: ${HasCodec},
HostConnectionLimit: ${HasHostConnectionLimit}")
private[builder] trait ClientConfigEvidence[HasCluster, HasCodec, HasHostConnectionLimit]
class ClientBuilder[Req, Rep, HasCluster, HasCodec, HasHostConnectionLimit] private[finagle](...) {
def build()(
implicit THE_BUILDER_IS_NOT_FULLY_SPECIFIED_SEE_ClientBuilder_DOCUMENTATION:
ClientConfigEvidence[HasCluster, HasCodec, HasHostConnectionLimit]
): Service[Req, Rep] = ...
}
val builder: ClientBuilder[Request, Response, Yes, Yes, Nothing] =
ClientBuilder()
.codec(Http())
.hosts("twitter.com:80")
builder.build()
Error:(24, 15) Builder is not fully configured: Cluster: com.twitter.finagle.builder.ClientConfig.Yes,
Codec: com.twitter.finagle.builder.ClientConfig.Yes, HostConnectionLimit: Nothing
builder.build()
^
Implicits	enable
DSLs
Type	evidence
Reduce	verbosity
Type	classes
Dependency	Injection	at	Compile	time
Extending	libraries
But	beware
Resolution	rules	can	be	difficult
Automatic	conversions
Do	not	overuse
Demo
Implicit	conversions	(a.k.a.	Implicit	views)
Implicit	parameters
Implicit	classes
Implicit	declarations
implicit def a2B(a : A) : B = ...
def method(implicit x : Int) = ...
implicit class X(y: Int)
implicit val x = ...
Scoping
Odersky	Explains
Lookup	precedence:
1.	 By	name	only,	without	any	prefix
2.	 In	"implicit	scope":
companion/package	object	of
the	source	type
its	parameters	+	supertype	and	supertraits
Demo
Scoping	and	resolving
Typeclass	uses
Ad-hoc	polymorphism
Extension	of	libraries
trait Numeric[T] extends Ordering[T] {
def plus(x: T, y: T): T
def minus(x: T, y: T): T
def times(x: T, y: T): T
...
}
Demo
Typeclass	for	JSON	Serialisation
Naive	with	subtyping
Typeclass	+	improvements
Recap
Implicits	are	powerful
Be	careful	with	conversions
Implicit	precedence:	first	look	local,	then	in	companion/package	object
Typeclasses	to	extend	libraries
Recap
Implicits	are	powerful
Be	careful	with	conversions
Implicit	precedence:	first	look	local,	then	in	companion/package	object
Typeclasses	to	extend	libraries
Questions?
Tim	Soethout	@	ScalaDays	2016	Berlin
References
	/	
Scala	documentation:
Book:	
Blog	
,	special	thanks	to	@Ichoran	and	@som-snytt
,	for	the	demo	slides
http://blog.timmybankers.nl
Slides Code
Java	Converters
Finding	Implicits
Scala	In	Depth
Effective	Scala
All	Things	Runnable
Scala	Gitter	channel
REPLesent

Implicits Inspected and Explained @ ScalaDays 2016 Berlin