Vala const从读写变量初始化

web*_*rc2 3 vala

我注意到Vala不允许你const从非const变量初始化变量.为什么是这样?这是故意的设计决定还是错误/遗漏?分别考虑这些Vala和C示例; 在C程序编译并按预期运行时,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;
//                 ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

apm*_*ell 6

const在Vala与C.具有不同的含义.AC变量const只是只读的,而const在Vala中,是一个编译时常量,就像它在C#中一样.