Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 21. Implicits > Rules for Implicit Conversions

21.4. Rules for Implicit Conversions

Implicit conversions are considered in three distinct situations:

  • If the type of an expression differs from the expected type:

    sqrt(Fraction(1, 4))
      // Calls fraction2Double since sqrt expects a Double
  • If an object accesses a nonexistent member:

    new File("README").read
      // Calls file2RichFile since File has no read method
  • If an object invokes a method whose parameters don’t match the given arguments:

    3 * Fraction(4, 5)
      // Calls int2Fraction since the * method of Int doesn't accept a Fraction

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial