Understanding Scala's Implicit Conversions and Parameters

Slide Note
Embed
Share

Scala's implicit keyword allows for type conversions by the compiler, inferring parameters when not directly stated, and is useful for integrating external code. Implicit conversion rules include marking, scope, one-at-a-time, and explicits-first rules. This guide provides examples and insights into leveraging implicit conversions effectively in Scala programming.


Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Implicit Conversions and Parameters in Scala Abraham Peediakal and Henry Dyer

  2. The implicit keyword What does it do? Provides type conversions to the compiler Infer parameters when they aren t given directly Useful for integrating outside code

  3. val button = new JButton button.addActionListener( new ActionListener { def actionPerformed(event: ActionEvent): Unit = { An Example: JButton println( pressed! ) } } ) // I m supposed to read this?

  4. val button = new JButton button.addActionListener( An Example: JButton (_: ActionEvent) => println( pressed! ) ) // function != ActionListener

  5. implicit def function2ActionListener(f: ActionEvent => Unit): ActionListener = new ActionListener { def actionPerformed(event: ActionEvent): Unit = f(event) }

  6. val button = new JButton button.addActionListener( An Example: JButton (_: ActionEvent) => println( pressed! ) ) // now it works!

  7. Implicit Conversion Rules 1. Marking Rule Only definitions marked implicit are considered

  8. Implicit Conversion Rules 2. Scope Rule An implicit definition must be in scope to be used Import Extends Note: Only needs brought into scope once

  9. Implicit Conversion Rules 3. One-at-a-Time Rule Compiler won t apply multiple conversions at once

  10. Implicit Conversion Rules 4. Explicits-first Rule If the code works, the compiler won t fix it

  11. An Example: Ints and Doubles val i: Int = 3.5 // that s illegal!

  12. implicit def doubleToInt(x: Double) = x.toInt An Example: Ints and Doubles // now it works! val i: Int = 3.5 // i: Int = 3

  13. val oneHalf = new Rational(1, 2) // oneHalf: Rational = 1/2 oneHalf + oneHalf An Example: Rationals // 1/1 oneHalf + 1 // 3/2 1 + oneHalf // that s illegal!

  14. implicit def intToRational(x: Int) = new Rational(x, 1) An Example: Rationals 1 + oneHalf // intToRational(1) + oneHalf // 1/1 + 1/2 // = 3/2

  15. Adding syntax to preexisting classes is easy! Map(1 -> one , 2 -> two , 3 -> three ) ^ Hey how do the arrows work?

  16. Adding syntax to preexisting classes is easy! package scala object Predef { class ArrowAssoc[A](x: A) { def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y) } implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x) }

  17. And now Implicit Classes

  18. case class Rectangle(width: Int, height: Int) An Example: Rectangles // shapes are great // constructors are lame // can we make this easier?

  19. implicit class RectangleMaker(width: Int) { An Example: Rectangles def x(height: Int) = Rectangle(width, height) } // automatically generates implicit conversion from int to RectangleMaker

Related


More Related Content