I’m having some trouble understanding why this code won’t work. I got it from 99 Scala Problems in the Binary Trees section (http://aperiodic.net/phil/scala/s-99/). It looks valid to me: the Node object is a companion object to the Node class, and it’s adding a constructor for leafs on the tree. But when I try compiling it, I get the following:
<console>:10: error: too many arguments for method apply: (value: T)Node[T] in object Node
def apply[T](value: T): Node[T] = Node(value, End, End)
If I remove both Ends, I don’t get any compile errors, but if I make a Node with a single value I get stuck in an infinite loop. So it looks like apply is constructing more Node objects, and isn’t associating itself with the Node class.
Any help is appreciated.
sealed abstract class Tree[+T]
case class Node[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T] {
override def toString = "T(" + value.toString + " " + left.toString + " " + right.toString + ")"
}
case object End extends Tree[Nothing] {
override def toString = "."
}
object Node {
def apply[T](value: T): Node[T] = Node(value, End, End)
}
Works for me (see below). Have you defined them in the same file?
Edit
From your comment: it looks like the
:loadcommand in the repl interprets each line in the file one by one, you can find the code for that here. However this doesn’t work using the REPL since (I believe) each line that is interpreted gets compiled in its own package. See this thread for more details. Perhaps this could be a future enhancement in the REPL. But in principle, there is nothing wrong with your code: using both:pastemode or just compiling withscalacworks fine.N.B. I couldn’t find any enhancement request on JIRA, so I created this issue