GNOME Bugzilla – Bug 621041
Non-constant Default Arguments Generate Incorrect C
Last modified: 2018-05-22 13:36:57 UTC
When a non-constant argument that references another argument is set in a method definition, any attempt to use that method results in a missing variable. For example void test (int[] a, int count = a.length) { } will not compile. Valac should probably treat this as an error, which it doesn't
These days, valac seems to compile your testcase, silently ignoring the non-constant default argument. I'm leaving this bug open though, since the current behaviour is still not desirable in my opinion.
(In reply to Michael 'Mickey' Lauer from comment #1) > These days, valac seems to compile your testcase, silently ignoring the > non-constant default argument. Default values for function parameters affect Vala code generation on the caller side. It is incorrect to say Vala silently ignores the non-constant default argument. This code: void main () { var test_array = new int[5]; test (test_array); } void test (int[] a, int count = a.length) { } Vala master produces this C: void _vala_main (void) { gint* test_array = NULL; gint* _tmp0_; gint test_array_length1; gint _test_array_size_; gint* _tmp1_; gint _tmp1__length1; _tmp0_ = g_new0 (gint, 5); test_array = _tmp0_; test_array_length1 = 5; _test_array_size_ = test_array_length1; _tmp1_ = a; _tmp1__length1 = a_length1; test (test_array, test_array_length1, _tmp1__length1); test_array = (g_free (test_array), NULL); } void test (gint* a, int a_length1, gint count) { } The caller is using the length for the count argument. If that is correct behaviour then this bug is fixed. > I'm leaving this bug open though, since the current behaviour is still not > desirable in my opinion. The more general question is whether a default parameter value should be derived from a required parameter.
I'm sorry, you are right. Vala optimized the (unused) function away, when I (successfully) compiled the example from the original post. The bug is still valid then. > The more general question is whether a default parameter value should be derived from > a required parameter. Indeed. To which my personal opinion is NO. Vala should either support that case or error out with an appropriate message.
-- 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/104.