The land
Vala provides enumerations. But these cannot be defined locally to a sub‑program. Constants can be defined locally to a sub‑program, but seems to not be treated as static expressions (pseudo constants so).
The case
I have some sub‑programs implemented as state machines built with switch statements. I use some switch (state) { … } and want to use some constant for the case statements, as in case initial_state: { … }. This is recommended I believe, as it is more readable and maintainable than using literal constants as in case 0: { … }.
I tried to defined these constants inside the sub‑program, using declarations like const int initial_state = 0;. But Vala complains at each case statements. I tried to define an enumerations for the states, as in enum State { initial_state, … };, but Vala reject this as a syntax error and seems to only allows enumerations declarations outside of sub‑programs.
So far, I have to either define all states enumerations as external to the sub‑programs, or else to define constants inside the sub‑programs, but then have to use if constructs instead of switch constructs, as it’s OK for the if condition expression, to not be static.
The question
Do Vala allows to define static constants (of a scalar type) locally to a sub‑program in some way?
This is actually an error from gcc, not valac. Using this example:
valac will generate:
gcc will say something like:
Interestingly, clang is fine with it (
valac --cc=clang ..., if you want to play with it).