weberc2
weberc2

Reputation: 7908

Vala const initialization from read-write variable

I noticed Vala doesn't let you initialize a const variable from a non-const variable. Why is this? Is this a deliberate design decision or a bug/omission? Consider these Vala and C examples respectively; the Vala program fails to compile while the C program compiles and runs as expected:

Vala

void main()
{
   const int constInt = 1;
   const int a = constInt;

   int plainInt = 0;
   const int b = plainInt;

   stdout.printf("A: %d\n", a);
   stdout.printf("B: %d\n", b);
}

// Compiler output:
//   test.vala:7.18-7.25: error: Value must be constant
//   const int b = plainInt;
//                 ^^^^^^^^

C

#include <stdio.h>

int main()
{
   const int constInt = 1;
   const int a = constInt;

   int plainInt = 0;
   const int b = plainInt;

   printf("A: %d\n", a);
   printf("B: %d\n", b);
   return 0;
}

Upvotes: 3

Views: 402

Answers (1)

apmasell
apmasell

Reputation: 7153

const has different meanings in Vala versus C. A C variable which is const is simply read-only, while const in Vala, is a compile-time constant, as it is in C#.

Upvotes: 6

Related Questions