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 647788 - Vala doesn't allow making fixed-sized arrays from constants
Vala doesn't allow making fixed-sized arrays from constants
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Parser
0.20.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on: 756376
Blocks:
 
 
Reported: 2011-04-14 15:35 UTC by Alexander Kurtz
Modified: 2018-05-22 14:01 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Test.vala (162 bytes, text/plain)
2011-04-14 15:35 UTC, Alexander Kurtz
Details
Demo.vala (200 bytes, text/plain)
2013-06-24 14:49 UTC, Alexander Kurtz
Details

Description Alexander Kurtz 2011-04-14 15:35:41 UTC
Created attachment 185964 [details]
Test.vala

Vala doesn't allow making fixed-sized arrays from constants. This is absolutely necessary since there are no preprocessor directives in Vala and most C libraries define symbolic names for these things.

The attached test program demonstrates the problem:

$ valac-0.12 Test.vala && ./Test 
Test.vala:4.13-4.19: error: syntax error, expected `]' or integer literal
		int array[MAXSIZE];
			  ^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
$
Comment 1 geert jordaens 2011-07-25 14:31:48 UTC
This should work as :

class Test : Object {
	const int MAXSIZE = 1024;
	public static void main(){
		var array = new int[MAXSIZE];
		stdout.printf("array has %d elements\n", array.length);
	}
}
Comment 2 Alexander Kurtz 2013-06-24 14:48:54 UTC
(In reply to comment #1)
> This should work as :
> 
> class Test : Object {
>     const int MAXSIZE = 1024;
>     public static void main(){
>         var array = new int[MAXSIZE];
>         stdout.printf("array has %d elements\n", array.length);
>     }
> }

Yes, but this isn't what I want. Your example creates a variable-size array which is allocated on the heap. I want a fixed-size array allocated on the stack (if used as a local variable) or in-line allocated (if used as a field in a struct or a class). See also: https://live.gnome.org/Vala/Tutorial#Arrays

I will attach a new example which may make this more clear. A real world example of the problem can be found here: http://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/vala/libpulse.vapi?id=97da92d894c2a29bc3ccf60dc904e4099b30f47e#n378

Best regards

Alexander Kurtz
Comment 3 Alexander Kurtz 2013-06-24 14:49:53 UTC
Created attachment 247649 [details]
Demo.vala
Comment 4 Alexander Kurtz 2013-06-24 14:53:47 UTC
This problem still exists in Vala 0.20:

$ cat Demo.vala 
const int LENGTH = 32;

public struct Demo {
	int array[LENGTH];
}

public static int main(string[] argv){
	Demo demo = Demo();
	stdout.printf("Array length is %d\n", demo.array.length);
	return 0;
}
$ valac --version
Vala 0.20.1
$ valac Demo.vala 
Demo.vala:4.12-4.17: error: syntax error, expected `]' or integer literal
	int array[LENGTH];
	          ^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
$ 

Best regards

Alexander Kurtz
Comment 5 oliver.steven 2017-02-01 23:57:43 UTC
Using Vala 0.30.1 this is still broken :(
Comment 6 GNOME Infrastructure Team 2018-05-22 14:01:12 UTC
-- 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/194.