2.3 implicits
Upcoming SlideShare
Loading in...5
×
 

2.3 implicits

on

  • 909 views

 

Statistics

Views

Total Views
909
Views on SlideShare
909
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as OpenOffice

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

2.3 implicits 2.3 implicits Presentation Transcript

  • Implicit conversions
  • Magic? String s = "!em esreveR"; System.out.println(s.reverse());val s: java.lang.String = "!em esreveR"println(s.reverse)=> Reverse me!
  • Magic? String s = "!em esreveR"; System.out.println(s.reverse());val s: java.lang.String = "!em esreveR"println(s.reverse)println(stringWrapper(s).reverse)=> Reverse me!
  • Magic? String s = "!em esreveR"; System.out.println(s.reverse());val s: java.lang.String = "!em esreveR"println(s.reverse)println(stringWrapper(s).reverse)println(new runtime.RichString(s).reverse)=> Reverse me!
  • implicitimplicit def stringWrapper(x: String) = new runtime.RichString(x)
  • scala.runtime.RichStringdef toArray : Array[Char]def toBoolean : Booleandef toByte : Bytedef toDouble : Doubledef toFloat : Floatdef toInt : Intdef toLong : Longdef toShort : Shortdef r : Regexdef lines : Iterator[java.lang.String]def * (n : Int) : java.lang.String...
  • scala.PredefA set of implicits Other nice functions byteWrapper(x: Byte) shortWrapper(x: Short) println(...) intWrapper(x: Int) charWrapper(c: Char) readLine(...) longWrapper(x: Long) floatWrapper(x: Float) error(...) doubleWrapper(x: Double) ... booleanWrapper(x: Boolean) stringWrapper(x: String) stringBuilderWrapper(x : StringBuilder) ...
  • Map and ArrowAssocval myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two")
  • Map and ArrowAssocval myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map.apply(Tuple2(1, "one"), Tuple2(2, "two"))
  • Map and ArrowAssocval myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map((1, "one"), (2, "two"))
  • Map and ArrowAssocval myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map(any2ArrowAssoc(1) -> "one", any2ArrowAssoc(2) -> "two") Map((1, "one"), (2, "two"))
  • scala.Predef.ArrowAssocimplicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x)class ArrowAssoc[A](x: A) { def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y)}val myMap = Map(1 -> "one", 2 -> "two")
  • Implicit rules!Marking Rule: Only definitions marked implicit are available.Scope Rule: An inserted implicit conversion must be in scope as a single identifier, or be associated with the source or target type of the conversion.Non-Ambiguity Rule: An implicit conversion is only inserted if there is no other possible conversion to insert.One-at-a-time Rule: Only one implicit is tried.Explicits-First Rule: Whenever code type checks as it is written, no implicits are attempted.