Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Scala syntax for a function taking any subtype of Ordered[A]?

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?

like image 405
asciiphil Avatar asked Mar 27 '09 20:03

asciiphil


People also ask

What is the syntax of defining method in Scala?

Syntax. def functionName ([list of parameters]) : [return type] = { function body return [expr] } Here, return type could be any valid Scala data type and list of parameters will be a list of variables separated by comma and list of parameters and return type are optional.

What does => mean in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

How do you create a function in Scala?

To define a function in Scala, you need to use the keyword def. Then add the name of your function which in our case will be favoriteDonut followed by an empty pair of parenthesis (). To specify the return type you need to use the colon : followed by the return type which in our case is a String.

What is the type of a function in Scala?

Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.


1 Answers

The equivalent of Haskell's type classes in Scala is done via implicits. There are two ways to do what you want

The first is with view bounds

scala> def lessThan[T <% Ordered[T]](x : T, y : T) = x < y
lessThan: [T](T,T)(implicit (T) => Ordered[T])Boolean

scala> lessThan(1,2)
res0: Boolean = true

The second is with an implicit parameter

scala> def lessThan[T](x : T, y : T)(implicit f : T => Ordered[T]) = x < y      
lessThan: [T](T,T)(implicit (T) => Ordered[T])Boolean

scala> lessThan(4,3)
res1: Boolean = false

The former is syntax sugar for the later. The later allows more flexibility.

like image 136
James Iry Avatar answered Oct 22 '22 19:10

James Iry