0

It said this:

List(1,4,2,3).sorted

can be done because in trait Ordering defined this:

implicit object Int extends IntOrdering with CachedReverse[Int]

but if class Int can use this implicit object,isn't it have to write this?

import scala.math.Ordering

In the example of book 《programming in scala》:

class PreferredPrompt(val preference: String)

object Greeter {
 def greet(name: String)(implicit prompt: PreferredPrompt) = {
  println("Welcome, " + name + ". The system is ready.")
  println(prompt.preference)
 }
}

object JoesPrefs {
 implicit val prompt = new PreferredPrompt("Yes, master> ")
}

the "implicit val prompt" defined in object JoesPrefs,so if want to write this:

Greeter.greet("Joe")

have to write:

import JoesPrefs._

so

implicit object Int extends IntOrdering with CachedReverse[Int]

defined in scala.math.Ordering

so shouldn't have write

import scala.math.Ordering

in class Int?Thanks!

wang kai
  • 1,291
  • 1
  • 10
  • 18
  • I hope the duplicate is clear enough, just a TL;DR; in your second example you need to `import JoesPrefs._` because **JoesPrefs** is not in the implicit scope. In the case of `sorted` since it expects an `Ordering[Int]` then all implicits the companion object **Ordering** are in scope automatically. – Luis Miguel Mejía Suárez May 14 '21 at 01:12

0 Answers0