这实际上是 gcc 的错误,而不是 valac。使用这个例子:
private void foo (int val) {
const int one = 1;
const int two = 2;
const int three = 3;
switch ( val ) {
case one:
GLib.debug ("One");
break;
case two:
GLib.debug ("One");
break;
case three:
GLib.debug ("Three");
break;
default:
GLib.debug (val.to_string ());
break;
}
}
valac 将生成:
void foo (gint val) {
static const gint one = 1;
static const gint two = 2;
static const gint three = 3;
gint _tmp0_;
_tmp0_ = val;
switch (_tmp0_) {
case one:
{
g_debug ("t.vala:8: One");
break;
}
case two:
{
g_debug ("t.vala:11: One");
break;
}
case three:
{
g_debug ("t.vala:14: Three");
break;
}
default:
{
gint _tmp1_;
gchar* _tmp2_ = NULL;
gchar* _tmp3_;
_tmp1_ = val;
_tmp2_ = g_strdup_printf ("%i", _tmp1_);
_tmp3_ = _tmp2_;
g_debug ("t.vala:17: %s", _tmp3_);
_g_free0 (_tmp3_);
break;
}
}
}
gcc 会说:
t.vala.c:25:3: error: case label does not reduce to an integer constant
t.vala.c:30:3: error: case label does not reduce to an integer constant
t.vala.c:35:3: error: case label does not reduce to an integer constant
有趣的是,clang 很好用(valac --cc=clang ...
如果你想玩它)。