After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 645696 - Cannot reference fields or methods in generic method's parameters
Cannot reference fields or methods in generic method's parameters
Status: RESOLVED NOTABUG
Product: vala
Classification: Core
Component: Methods
0.11.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-03-26 00:15 UTC by Jim Nelson
Modified: 2011-03-28 22:42 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Jim Nelson 2011-03-26 00:15:34 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;
}
Comment 1 Jürg Billeter 2011-03-27 15:25:31 UTC
(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?
Comment 2 Jim Nelson 2011-03-28 19:43:02 UTC
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.