Generics not so generic !!

Posted by Aymen on Stack Overflow See other posts from Stack Overflow or by Aymen
Published on 2010-05-16T11:56:24Z Indexed on 2010/05/16 12:00 UTC
Read the original article Hit count: 90

Filed under:
|

Hi I tried to implement a generic binary search algorithm in scala. Here it is :

type Ord ={
def <(x:Any):Boolean
def >(x:Any):Boolean
}
def binSearch[T <: Ord ](x:T,start:Int,end:Int,t:Array[T]):Boolean = {
if (start > end) return false
val pos = (start + end ) / 2
if(t(pos)==x)         true
else if (t(pos) < x)  binSearch(x,pos+1,end,t)
else                binSearch(x,start,pos-1,t)
}

everything is OK until I tried to actually use it (xD) :

binSearch(3,0,4,Array(1,2,5,6))

the compiler is pretending that Int not a member of Ord, well what shall I do to solve this ? Thanks

© Stack Overflow or respective owner

Related posts about scala

Related posts about generics