The19thFighter
The19thFighter

Reputation: 103

Are constant expressions possible in Vala?

In C, it's possible to use macros or constexpr to evaluate simple constant expressions and use them to precompute certain mathematical operations at compile time.

In Vala, it seems like it's not possible to write constant expressions.

Following is an example of what I'm trying to achieve in C:

// From devkitPro's gba_video.h
#define RGB8(r,g,b) ( (((b)>>3)<<10) | (((g)>>3)<<5) | ((r)>>3) )

const uint16_t palette[] = {
    RGB8(0x10,0x10,0x10),
    RGB8(0x20,0x20,0x20)
};

In Vala, I tried recreating the same:

public class App {
    public static uint16 RGB8 (uint8 r, uint8 g, uint8 b) {
        return (((b >> 3) << 10) | ((g >> 3) << 5) | (r >> 3));
    }

    static uint16 palette[2] = {
        RGB8(0x10,0x10,0x10),
        RGB8(0x20,0x20,0x20)
    };
}

However adding const to just palette throws the following error:

error: Value must be constant

And adding const to the RGB8 definition gives:

error: syntax error, expected `('

Upvotes: 0

Views: 24

Answers (0)

Related Questions