Skip to main content

Literals

A literal is a value written directly in source code. This page covers the syntax for all literal forms in Komplete Script.

Integer Literals

Integer literals represent whole numbers and are of type Int.

Decimal

var a = 42
var b = 0

Hexadecimal

Hexadecimal literals are prefixed with 0x or 0X. Both uppercase and lowercase digits are accepted.

var red   = 0xFF0000
var color = 0xDEADBEEF

Floating-Point Literals

Floating-point literals represent fractional numbers and are of type Float. They require at least one digit on each side of the decimal point.

Decimal

var pi   = 3.14159
var zero = 0.0

Exponent Notation

Use e or E followed by an optional sign (+ or -) and an integer exponent. The value equals the base multiplied by 10 raised to the given exponent.

var a = 1.5e10    // 1.5 × 10¹⁰
var b = 1.5e+10 // same as above
var c = 1.5e-3 // 1.5 × 10⁻³ → 0.0015
var d = 2.0E6 // 2.0 × 10⁶
var e = 2.0E+6 // same as above
var f = 2.0E-6 // 2.0 × 10⁻⁶

Special Values

Two named constants are available for special floating-point values:

ConstantDescription
nanNot a Number — result of undefined operations such as 0.0 / 0.0
infinityPositive infinity
var a: Float = infinity
var b: Float = nan

String Literals

String literals are enclosed in double quotes (").

var greeting = "Hello, world!"
var empty = ""

See String in the Standard Library for escape sequences, Unicode codepoints, and string interpolation.

Boolean Literals

Boolean literals are of type Bool.

var yes = true
var no = false

Array Literals

Array literals are written as a comma-separated list of values enclosed in square brackets ([]). A trailing comma after the last element is allowed.

var scores  = [10, 20, 30]
var names = ["Alice", "Bob",] // trailing comma ok
var empty: [Int] = [] // type annotation required for empty arrays

Map Literals

Map literals are written as a comma-separated list of key: value pairs enclosed in square brackets. A trailing comma is allowed. An empty map literal is written as [:] to distinguish it from an empty array.

var ages    = ["Alice": 30, "Bob": 25]
var lookup = [1: "one", 2: "two",] // trailing comma ok
var empty: [String: Int] = [:] // type annotation required for empty maps

Nil Literal

nil is the literal for the absence of a value. It can be assigned to any optional type.

var name: String? = nil

See Optionals for details.