Closed
Description
Compiler version
3.1.0
Minimized code
def f[T <: Ordered[T]](t: T): T = t
f(1)
Output
Found: (1 : Int)
Required: Ordered[
Ordered[
Ordered[
Ordered[
Ordered[
Ordered[
Ordered[
Ordered[
Ordered[
Ordered[Ordered[Ordered[Ordered[Ordered[Ordered[...[...]]]]]]]
]
]
]
]
]
]
]
]
]
One of the following imports might fix the problem:
import math.BigDecimal.int2bigDecimal
import math.BigInt.int2bigInt
Expectation
// this is also what Scala 2 gives:
inferred type arguments [Int] do not conform to method f's type parameter bounds [T <: Ordered[T]]
Info
Hello all, I accidentally came across this while reading Chapter 18 "Type Parameterization" of "Programming in Scala, 5th Edition". At the end of the chapter there is an example of
def orderedMergeSort[T <: Ordered[T]](xs: List[T]): List[T] = ...
then the book demonstrates why this cannot be used to sort a list of integers because Int
is not a subtype of Ordered[Int]
:
// this is what the book says should happen (also what Scala 2 gives):
scala> val wontCompile = orderedMergeSort(List(3, 2, 1))
<console>:5: error: inferred type arguments [Int] do
not conform to method orderedMergeSort's type
parameter bounds [T <: Ordered[T]]
val wontCompile = orderedMergeSort(List(3, 2, 1))
^
I thought the error message looked really strange, and obviously different from the book. So I minimized it.
Providing a type parameter gives a much better message:
scala> f[Int](1)
-- Error:
1 |f[Int](1)
| ^
| Type argument Int does not conform to upper bound Ordered[Int]
Also doing one of the mentioned imports fixes the problem too.
Wasn't sure if it is a bug. I asked in Discussions, @SethTisue encouraged me to ask in Scala Users, where @smarter encouraged me to report it.