KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to write a function that works on any Scala type with a total ordering (i.e. I can use '<' on it). What's the syntax for that? The best I've come up with is def lessThan[T <: Ordered[T]](x: T, Y: T) = x < y That doesn't work, though, when I try using it from the REPL: scala> lessThan(1, 2) <console>:8: error: inferred type arguments [Int] do not conform to method lessThan's type parameter bounds [T <: Ordered[T]] lessThan(1, 2) ^ scala> import runtime._ import runtime._ scala> lessThan(new RichInt(1), new RichInt(2)) <console>:8: error: inferred type arguments [scala.runtime.RichInt] do not conform to method lessThan's type parameter bounds [T <: Ordered[T]] lessThan(new RichInt(1), new RichInt(2)) Essentially, I believe I want the equivalent of this Haskell code: lessThan :: (Ord a) => a -> a -> Bool lessThan x y = x < y I'm using scala 2.7.3 on a Debian system. What am I missing, and where?
Tags (comma-separated)
Save Edits
Cancel