Constants
Examples in
V
const (
pi = 3.14
world = '世界'
)
println(pi)
println(world)
// Constants are declared with const. They can only be defined at the module level (outside of functions).
// Constant values can never be changed.
// V constants are more flexible than in most languages. You can assign more complex values:
struct Color {
r int
g int
b int
}
pub fn (c Color) str() string { return '{$c.r, $c.g, $c.b}' }
fn rgb(r, g, b int) Color { return Color{r: r, g: g, b: b} }
const (
numbers = [1, 2, 3]
red = Color{r: 255, g: 0, b: 0}
blue = rgb(0, 0, 255)
)
println(numbers)
println(red)
println(blue)
enum Color {
red green blue
}
mut color := Color.red
// V knows that `color` is a `Color`. No need to use `color = Color.green` here.
color = .green
println(color) // "1" TODO: print "green"?