GNOME Bugzilla – Bug 530030
Declaring "int a, b;" as member variable of a class results in parsing error
Last modified: 2018-05-22 13:05:27 UTC
Compiling example below public class Sample { int a, b; static int main (string[] args) { int x, y; return 0; } } gives /home/haitsma/blah.vala:3.7-3.7: error: syntax error, expected declaration
Created attachment 110390 [details] [review] suggested patch There is the same problem with const declarations, which are parsed differently from normal fields. Another test case: using GLib; public class Sample { static const int i = 1, j = 2; static int a, b; static int main (string[] args) { int x, y; a = 3; b = 4; stdout.printf("%d ", i); stdout.printf("%d ", j); stdout.printf("%d ", a); stdout.printf("%d ", b); stdout.printf("\n"); // Expected: // 1 2 3 4 return 0; } } Suggested patch is attached. Perhaps specialised singleton implementations of collections could be added to libgee, like in Java Collections [1]. If you agree then I'll file a new bug and patch for that :) [1] http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#singleton(java.lang.Object)
Also see ideas in http://mail.gnome.org/archives/vala-list/2008-April/msg00135.html
Confirming, it can make sense in some cases to declare multiple fields on the same line. Thanks for the patch, Jared, I'll try to review it as soon as possible.
I'd like to avoid creating a new array list for each declaration as this costs about 10% of the overall performance. We could handle it similarly to parse_local_variable_declarations but that might make the code less clear. Maybe there is an other solution that keeps the code simple and is still performant.
Does that really affect the performance of the entire compiler that much? I really don't see any other nice way of implementing this. :/ I wrote an implementation of a singleton collection as mentioned in comment 1, which seems to have slightly better performance, but it seems like a bit of a premature optimisation. If you like I can clean it up a bit and upload that here or as a separate bug.
*** Bug 592886 has been marked as a duplicate of this bug. ***
Which version of valac does this bug affect? I have 0.12 on Ubuntu 11.04 "Natty Narwhal" Alpha 1 and still get this bug in a file where I wrote test code to basically draw two cube-oids on the screen (without any toolkit): using GLib; public class Box : GLib.Object { unsigned int Left; // unsigned int Top; //Left, Top, Rear coordinate group unsigned int Rear; // unsigned int Right; // unsigned int Bottom; //Right, Bottom, Front coordinate group unsigned int Front; // public Box (int l, int t, int r1, int r2, int b, int f) { //Constructor l = Left; t = Top; r1 = Rear; r2 = Right; b = Bottom; f = Front; } public int main (string[] args) { var box_1 = new Box (0, 0, 0, 640, 480, 640); var box_2 = new Box (0, 0, 0, 600, 400, 600); return 0; } } I hope the patch gets applied upstream soon as member variables are essentially the guts of classes.
*** Bug 648184 has been marked as a duplicate of this bug. ***
Created attachment 198017 [details] [review] Allows declaration of multiple fields/constants on the same line
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/6.