Reputation: 380
I am writing Scala code to search a QuadTree. I'm fairly new to Scala, so this may be a simple question: Why do I get a error: value rec not a member of QuadTree
when I try to use tree.rec
in a function, BUT if I ask the REPL to evaluate tree.rec
, it successfully returns the data?
Anyway, I've got these classes to start with:
case class Rectangle (minx: Int, maxx: Int, miny: Int, maxy: Int)
abstract class QuadTree
case class Node (value : Int, nw : QuadTree, ne: QuadTree, sw: QuadTree, se: QuadTree, rec: Rectangle) extends QuadTree
case class Leaf (value : Int, rec: Rectangle) extends QuadTree
case class Empty (value: Int, rec: Rectangle) extends QuadTree
So I'm defining a Rectangle, and then a QuadTree, which can has a value, and either contains a Node, a Leaf, or Empty.
Next I have a function that checks to see if two rectangles overlap called rectangles_Overlap
, which returns True
if two input Rectangles overlap, and returns False
if there is no overlap.
If I define
val query = Rectangle(1, 8, 8, 16)
val tree1 = Node(13,Node(12,Leaf(7,Rectangle(1,4,12,16)),Leaf(3,Rectangle(4,8,12,16)),Leaf(2,Rectangle(1,4,8,12)),Empty(0,Rectangle(4,8,8,12)),Rectangle(1,8,8,16)),Empty(0,Rectangle(8,16,8,16)),Empty(0,Rectangle(1,8,1,8)),Leaf(1,Rectangle(8,16,1,8)),Rectangle(1,16,1,16))
If I want to get the size of the tree, I can type into the REPL
scala> tree1.rec
res0: Rectangle = Rectangle(1,16,1,16)
I can also use my rectangles_Overlap
method to see if the query Rectangle overlaps with the tree Rectangle.
scala> rectangles_Overlap(query, tree1.rec)
res1: Boolean = true
But if I try to use rectangles_Overlap(query, tree1.rec)
in another function, its unhappy!
def queryBoolean(query: Rectangle, tree: QuadTree): Boolean = {
if(rectangles_Overlap(query, tree.rec)) {
println("Yay they overlap so I can do other stuff...")
// I want to add other code here after I get this working
true
} else {
println("Nah, they don't overlap, don't need to do anything")
false
}
}
And then call the function with queryBoolean(query, tree1)
, I get this error:
<console>:17: error: value rec is not a member of QuadTree
if (rectangles_Overlap(query, tree.rec)) {
So my question again is: Why can I evaluate tree1.rec
in the REPL, but cannot use tree.rec
in a function without an error?
My original thought was that perhaps I was overloading something... but rec
is always a Rectangle
in all of the case clase ... extends QuadTree
. So I don't believe I am overloading anything?
Upvotes: 0
Views: 71
Reputation: 5000
That is true. rec i.e. type of Rectangle, is not a member of QuadTree. If you change from QuadTree to Node, it will work. Otherwise it must be there in QuadTree too.
def queryBoolean(query: Rectangle, tree: Node): Boolean
Add to QuadTree as val (val is needed to access it), and then you need to override and call super constructor as below:
abstract class QuadTree(val rec: Rectangle)
case class Node (value : Int, nw : QuadTree, ne: QuadTree, sw: QuadTree, se: QuadTree, override val rec: Rectangle) extends QuadTree(rec)
case class Leaf (value : Int, override val rec: Rectangle) extends QuadTree(rec)
case class Empty (value: Int, override val rec: Rectangle) extends QuadTree(rec)
Upvotes: 2
Reputation: 4125
This is because rec
is not defined for class QuadTree
, but only for its subclasses, therefore there is no value to call. The reason that this works properly on REPL is because of the way it is defined. You define it as
val tree1 = Node(/*some params*/)
Which means it has type Node
, instead of QuadTree
. As Node
has a defined rec
parameter, everything works out fine. However in the method, you pass in a raw QuadTree
, so no rec is defined.
To get it to work, either add rec
to the QuadTree
class, or pass in a specific subtype, which has the correct parameters defined.
Upvotes: 1