GNOME Bugzilla – Bug 645696
Cannot reference fields or methods in generic method's parameters
Last modified: 2011-03-28 22:42:57 UTC
In Vala 0.11.7, I have this code: class Node { public int val; public Node(int val) { this.val = val; } } int compare<Node>(Node a, Node b) { return a.val - b.val; } void main() { Node a = new Node(1); Node b = new Node(2); debug("%d", compare(a, b)); } The compiler complains: test.vala:11.12-11.16: error: The name `val' does not exist in the context of `Node' return a.val - b.val; This works fine if I remove the generic: int compare(Node a, Node b) { return a.val - b.val; }
(In reply to comment #0) > int compare<Node>(Node a, Node b) { > return a.val - b.val; > } In this method, the parameter type 'Node' refers to the generic type parameter 'Node'. There is no connection between the generic type parameter 'Node' and the class 'Node'. Could you elaborate how exactly you expected this to work?
Okay -- I dug in and see the problem now. I went down this path because of a faulty assumption. I'm trying to use Gee.TreeSet<Node> and assumed its constructor wanted a CompareFunc<Node>?. In fact, it only asks for a CompareFunc?. Apparently this is legal and the only valid signature for it is: int comparator(void *a, void *b) (Am I correct in thinking this?) My original bug report was due to trying to fulfill CompareFunc<Node> these ways: int comparator(Node a, Node b) and: it comparator<Node>(Node a, Node b) The first gave an error message that sounded like the method signature syntax was incorrect. The compiler didn't complain about the second's method signature but its implementation, which made me think I was on the right track. My goof.