scala’s implicits

36
Scala’s Implicits Pablo Francisco Pérez Hidalgo (@pfcoperez) github.com/pfcoperez

Upload: pablo-francisco-perez-hidalgo

Post on 15-Apr-2017

98 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Scala’s implicits

Scala’s Implicits

Pablo Francisco Pérez Hidalgo (@pfcoperez)github.com/pfcoperez

Page 2: Scala’s implicits

Scala’s Implicits Implicit Values

Pablo Francisco Pérez Hidalgo (@pfcoperez)github.com/pfcoperez

Page 3: Scala’s implicits

Scala 101

Page 4: Scala’s implicits

Programming 101: Formal vs Actual parameters

Page 5: Scala’s implicits

Scala 101: Lists of parameters

Page 6: Scala’s implicits

Scala 101: Methods are not functions

Page 7: Scala’s implicits

… but they are kind of cousins.

Page 8: Scala’s implicits

Know your implicit things

Page 9: Scala’s implicits

The compiler’s toy box: Implicit parameters Just for methods

Page 10: Scala’s implicits

The compiler’s toy box: Implicit parameters Just for methods

Page 11: Scala’s implicits

The compiler’s toy box: Implicit parameters Just for methods

Page 12: Scala’s implicits

The compiler’s toy box: Implicit conversions

Conversion:Domain = R

-10.0, 1.3, 9.2, ….

Conversion, I am your SOURCE TYPE

And, I am your TARGET TYPE

Page 13: Scala’s implicits

Implicit conversions: A silly example

Page 14: Scala’s implicits

Facts

Actual implicit parameters are values.

Formal implicit parameters are all those in the implicit parameter list.

Only methods can have - a single - implicit parameter list.

Functions are values.Guinea Pigs are pets, not food.

Implicit conversions are specially tagged functions, that is values.

Methods are not functions but can be assigned to function values thus becoming functions so they can become implicit conversions.

Page 15: Scala’s implicits

Know your implicit context

Page 16: Scala’s implicits

The implicit context

Compound Noun (singular)The place from where the magic unicorns come...

Page 17: Scala’s implicits

The implicit contexterror: ambiguous implicit values: both value c of type => C and value d of type => C

WHY?! WHAT?!Where did that implicit value came from???!!!

I DONT KNOW MORTY! BUT… RUN! RUN! RUN!

Page 18: Scala’s implicits

The implicit context is, once tamed, is... MOSTLY HARMLESSImplicit Context (n)The implicit context at a given point of your source code is the set of implicit values available for the compiler to perform type conversions or complete function calls when that point of code is providing an unexpected type object or missing some parameter in a function call.

That is… a mere backpack full of values for the compiler to use.

Page 19: Scala’s implicits

Identifying the limits of your implicit context

Page 20: Scala’s implicits

Identifying the limits of your implicit context: Scope

Page 21: Scala’s implicits

Identifying the limits of your implicit context: Implicit parameters

Page 22: Scala’s implicits

Identifying the limits of your implicit context: Target & goal companion objects

Conversion Source Type Target Type

A B

B A

B A

Page 23: Scala’s implicits

Patterns, let’s take it easy

Page 24: Scala’s implicits

Representing context

Page 25: Scala’s implicits

Representing context

Page 26: Scala’s implicits

Basic exercise

Page 27: Scala’s implicits

Fighting type erasure:The compiler & implicit values to the rescue

Page 28: Scala’s implicits

Adventure time

Page 29: Scala’s implicits

Problem

Page 30: Scala’s implicits

Old-school solution

Traditional OOP

Solution

Page 31: Scala’s implicits

Type classes to therescue

Maintainable += Animal

Maintainable += Equipment

Page 32: Scala’s implicits

Type classes to therescue

Page 33: Scala’s implicits

That wasn’t just a hack...

Page 34: Scala’s implicits

A magnetic way of solving the same problem...

Page 35: Scala’s implicits

A magnetic way of solving the same problem...

Page 36: Scala’s implicits