• Save
Scala implicits
Upcoming SlideShare
Loading in...5
×
 

Scala implicits

on

  • 1,598 views

Introduction to

Introduction to

Statistics

Views

Total Views
1,598
Views on SlideShare
1,591
Embed Views
7

Actions

Likes
3
Downloads
0
Comments
0

2 Embeds 7

http://www.slideshare.net 4
http://localhost 3

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Scala implicits Scala implicits Presentation Transcript

  • Implicits 2 Features, 1 Keyword Nick Partridge @nkpart
  • 2 Features • Implicit Conversions • Implicit Parameters
  • Conversions • int a = 5.3f; // Implicit conversion
  • Conversions • val a: A = ...; • val b: B = a; // Compile Fail. Of course.
  • Conversions • val a: A = ...; • implicit def (a: A): B = ...; • val b: B = a; • // Profit.
  • Conversions Compiler searches the ‘implicit scope’ • val b: B = a; • val b: B = bToA(a);
  • Pimp My Library Live Demo.
  • Conversions • Also triggered by a method search case class Foo(s: String) { def pl = s + “es” }
  • Conversions implicit def toFoo(s: String) = Foo(s) “peach”.pl // == “peaches” toFoo(s).pl
  • Not Transitive Implicit A => B Implicit B => C Does not mean Implicit A => C
  • View Bounds Live coding!
  • The End. Of Part 1.
  • Parameters
  • Parameters • Scala functions have parameter groups • def foo(a: Int)(b: Int)(c: Int) = ... • foo(1)(2)(3)
  • Parameters • The *last* group can be `implicit`. • def mungeData(d: Data)(implicit m: Munger) • mungeData(myData)(myMunger)
  • Parameters • implicit val myMunger = new Munger • mungeData(myData)(myMunger) Compiler figures out the last group
  • Parameters • def mungeData[D](d: D)(implicit m: Munger[D]) • mungeData(“foo”) // Compiler finds a Munger[String]
  • Parameters • Higher kinds! • def mungeData[M[_], D](d: D)(implicit m: Munger[D], each: Each[M]) = ... • mungeData(List(1,2,3)) • Munger[Int] • Each[List]
  • Live demo! Monoid!
  • Conversions Parameters
  • Where to get the ‘implicits’ from?
  • Implicit Search Scope • Similar to value resolution scope • local variables, values in current object, super classes. • Imported object members • Function parameters
  • Implicit Search Scope // COMPANION OBJECTS trait Monoid[T] object Monoid { // SAME NAME implicit val s = new Monoid[String] } // `s` will be available anywhere a Monoid[String] is needed. Without imports.
  • Implicit Search Scope def foo[T](t: T)(implicit m: Monoid[T]) = { // m is available for any implicit parameters needed within this method body }
  • More! • Anonymous functions • implicit r => { ... } • Import instances • case class Z(a: A) { implicit ia = a } • val z = new Z(someA) • import z._
  • Wonderful and Terrible.
  • La fin.