In the following example, I could compare two String
s using view bound
but not upper bound
even though on REPL, the <
method works for String
. Is it because String is not a subclass of Ordered[T] but there is an implicit conversion from String to Ordered[String]?
//view bound example. For String, It seems to work because I suppose there is an implicit conversion from String to Ordered[String]
scala> class Person[T <% Ordered[T]](val fn:T, val ln:T){
| def greater = if(fn > ln) fn else ln
| }
defined class Person
scala> val p1 = new Person("manu", "chadha")
p1: Person[String] = Person@f95d64d
scala> p1.greater
res0: String = manu
scala> val p2 = new Person("anita", "chadha")
p2: Person[String] = Person@30cafd13
scala> p2.greater
res1: String = chadha
//upper bound example. It doesn't work for String. Is it because String is not a subclass of Ordered[T] but there is an implicit converstion from String to Ordered[Strig]
scala> class Person2[T <: Ordered[T]](val fn:T, val ln:T){
| def greater = if(fn > ln) fn else ln
| }
defined class Person2
scala> val p3 = new Person2("manu", "chadha")
<console>:12: error: inferred type arguments [String] do not conform to class Person2's type parameter bounds [T <: Ordered[T]]
val p3 = new Person2("manu", "chadha")
^
<console>:12: error: type mismatch;
found : String("manu")
required: T
val p3 = new Person2("manu", "chadha")
^
<console>:12: error: type mismatch;
found : String("chadha")
required: T
val p3 = new Person2("manu", "chadha")
^
I suppose view bounds are deprecated. So how does the following example work in REPL?
scala> "manu" > "Manu"
res2: Boolean = true