Constants and Literals
What Constants Are¶
Constants define reusable literal values.
Constants do not have explicit type annotations. VDL infers their value kind from the literal.
Correct:
Invalid:
Scalar Literals¶
VDL supports these scalar literal forms:
const textValue = "hello"
const intValue = 42
const floatValue = 3.14
const trueValue = true
const falseValue = false
Strings use double quotes.
Object Literals¶
Object literals use braces and space-separated key/value entries.
There are no colons and no commas.
Object keys must be unique inside the same object.
Nested Object Literals¶
Array Literals¶
Array items are separated by whitespace.
All items in an array literal must have the same value kind.
Invalid:
Constant References¶
Constants can reference other constants.
References use the constant name directly.
Enum Member References¶
Constants can reference enum members with EnumName.MemberName.
enum ProductStatus {
Draft = "draft"
Published = "published"
}
const defaultStatus = ProductStatus.Draft
The referenced enum and member must exist.
Object Spreads¶
Object literals can spread another constant object.
const baseConfig = {
host "localhost"
port 8080
}
const productionConfig = {
...baseConfig
host "api.example.com"
}
Only constants whose value is an object literal can be used in object spreads.
Spreads use the Name form. Name.Member is not valid for object spreads.
Constants In Annotations¶
Because annotation arguments are data literals, constants can also help share annotation values.
Whether a plugin understands a particular annotation is plugin-specific.
Naming¶
Constant names should use camelCase.