0
sealed abstract class Node[Index: Ordering](var children: TreeMap[Index, Node[Index]],
                                        val idx: Index = null)
case class NotLeaf[Index: Ordering](override var children: TreeMap[Index, Node[Index]] = TreeMap.empty[Index, Node[Index]],
                          override val idx: Index = null) extends Node(children, idx)

object TrieTree {
  def main(args: Array[String]) {
    val root: Node[Char] = NotLeaf[Char]()
  }
}

why won't this compile? It says: No implicit Ordering defined for Index.

asuka
  • 1
  • Please provide a complete code snippet that we can compile, otherwise you are needlessly complicating the task of anyone wanting to help. See http://stackoverflow.com/help/mcve – Régis Jean-Gilles Sep 15 '15 at 08:09

1 Answers1

2

The syntax Node[Index: Ordering] tells the compiler to find an implicit Ordering[Index] in scope. Since it can't find one, your program fails to compile.

The [A: B] syntax is really syntactic sugar. Your Node class is being treated like this:

sealed abstract class Node[Index](var children: TreeMap[Index, Node[Index]], val idx: Index = null)(implicit o: Ordering[Index])

Notice how the compiler adds an implicit parameter to the end of the constructor parameters.

The solution, then, is for you to define your own implicit val indexOrdering: Ordering[Index] = ???. See the scaladocs for Ordering for help with that.

Zeimyth
  • 1,299
  • 12
  • 19
  • Index is a type I defined, that has a view bound of ```implicit ord: Ordering[Index]```, I want make the caller provide the ordering. eg: I use Char as Index to call it, and scala has Ordering[Char], how can I achieve that – asuka Sep 15 '15 at 06:36
  • It would be helpful then if you would include a code snippet for how you define Index. Also, try reading up about how [Scala finds implicit values](http://stackoverflow.com/questions/5598085/where-does-scala-look-for-implicits) – Zeimyth Sep 15 '15 at 14:37