Reputation: 478
i am trying to define ordering on data types with Option[Any] but getting error as "method compare overrides nothing"
below is code:-
implicit def ordering[Option[A]]: Ordering[Option[A]] = new Ordering[Option[A]] {
override def compare[A](x: A, y: A)(implicit ord: Ordering[A]): Int = {
ord.compare(x,y)
}
}
this whole code to build binary search tree using mutable list with generic any type
import scala.collection.mutable.ListBuffer
import math.Ordering
import Numeric.Implicits._
class treeUsingList[A]( treeList:ListBuffer[Option[Any]] ){
println("A greeter is being instantiated")
var lst = treeList
var size= treeList.length
lst=buildtree(lst)
implicit def ordering[Option[A]]: Ordering[Option[A]] = new Ordering[Option[A]] {
override def compare[A](x: A, y: A)(implicit ord: Ordering[A]): Int = {
ord.compare(x,y)
}
}
def buildtree[A](inputLst:ListBuffer[Option[A]]): ListBuffer[Option[Any]]={
var j=0
var k=0
var lst = ListBuffer[Option[Any]]()
for (i<- inputLst){
lst = this.addNode(lst, i , 0)
}
lst
}
def addNode[A](treeLst:ListBuffer[Option[A]] , item :A , idx:Int)(implicit order: Ordering[Option[A]]):ListBuffer[Option[A]] ={
if (order.lt(treeLst(idx) , Some(item)) ){
if(treeLst.length < idx*2+1){
while(treeLst.length < idx*2+1){
treeLst.append(None)
}
treeLst.append(Some(item))
}
else{
if(treeLst(idx*2+2)==None){
treeLst(idx*2+2)= Some(item)
}
else{
addNode(treeLst,item ,idx*2+2 )
}
}
}
treeLst
}
}
object mainTree{
var b = scala.collection.mutable.ListBuffer[Option[Any]](Some(5),Some(2),Some(3),Some(1),Some(7))
var atree = new treeUsingList[Int](b)
print(atree)
}
Upvotes: 0
Views: 807
Reputation: 96
I think no need define ordering object. You can use math.Ordering.OptionOrdering
.
An instance of OptionOrdering[Int]
can be create with math.Ordering.Option[Int]
Following code can compile, but still have some bug.
import scala.collection.mutable.ListBuffer
import math.Ordering._
import Numeric.Implicits._
class treeUsingList[A]( treeList:ListBuffer[Option[A]])(implicit order: Ordering[Option[A]]){
println("A greeter is being instantiated")
var lst = treeList
var size= treeList.length
lst=buildtree(lst)
def buildtree(inputLst:ListBuffer[Option[A]]): ListBuffer[Option[A]]={
var j=0
var k=0
var lst = ListBuffer[Option[A]]()
for (i<- inputLst){
lst = addNode(lst, i , 0)
}
lst
}
def addNode(treeLst:ListBuffer[Option[A]] , item :Option[A] , idx:Int)(implicit order: Ordering[Option[A]]):ListBuffer[Option[A]] ={
if (order.lt(treeLst(idx) , item) ){
if(treeLst.length < idx*2+1){
while(treeLst.length < idx*2+1){
treeLst.append(None)
}
treeLst.append(item)
}
else{
if(treeLst(idx*2+2)==None){
treeLst.update(idx*2+2, item)
}
else{
addNode(treeLst,item ,idx*2+2 )(order)
}
}
}
treeLst
}
}
object mainTree{
var b = scala.collection.mutable.ListBuffer[Option[Int]](Some(5),Some(2),Some(3),Some(1),Some(7))
var atree = new treeUsingList[Int](b)(math.Ordering.Option[Int])
print(atree)
}
Upvotes: 1
Reputation: 51271
It's a bit difficult to determine what you're trying to achieve because the code is doing a number of things that are confusing or just plain wrong.
override def compare[A]( . . .
^^^
This type parameter shadows the one defined in the class
definition, which means the A
in the compare()
method is different from the A
outside of it.
override def compare[A](x: A, y: A)(implicit ord: Ordering[A]): Int = . . .
^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
The trait
member you're trying to override doesn't have a 2nd parameter group so this doesn't match.
def ordering[Option[A]]: . . .
^^^^^^^^^
Here you're creating a type parameter called "Option[A]
" which is a goofy name for a type parameter and it doesn't mean what you think it does.
So here's something that compiles. Probably not what you're looking for, but it compiles, and that's not a bad place to start.
class treeUsingList[A](treeList:ListBuffer[Option[Any]]) {
. . .
implicit def ordering: Ordering[Option[A]] = new Ordering[Option[A]] {
override def compare(x: Option[A], y: Option[A]): Int = {
implicitly[Ordering[Option[A]]].compare(x, y)
//probably better to unpack x and y before comparing
}
}
. . .
}
Upvotes: 1