Keywords and reserved words
Keywords are the words of Ant's grammar:
break continue defer else export false for func go if import
in interface is let null return select true try type var void whileReserved words are kept free so that they stay available and so that habits from JavaScript produce an error instead of an identifier. Self is the one with a use today: it names the satisfying type inside an interface.
as async await catch class const enum finally function
match Self switch this throw undefinedDetails
- A keyword or reserved word cannot name a variable, a function, a type, or a parameter.
- A field can have any name. After
.or?., and before:in a record type, a record or map literal, or a record destructuring, a keyword or reserved word is a plain name. Selfhas one use: in the signatures of aninterface, it names the type that satisfies it.- In a test file,
testis a keyword as well. Everywhere else it is a plain name. - Identifiers are case-sensitive, so
ClassandTypeare ordinary names.
Basic examples
A keyword cannot name a variable:
let type = "click" // error: 'type' cannot be a variable nameA reserved word cannot either:
let class = "button" // error: 'class' cannot be a variable nameA keyword works as a field name:
type Event = { type: string, target: string }
let event = Event{ type: "click", target: "save" }
event.type // "click"Scope
Field access
After . and ?., a keyword or reserved word is a field name:
type Label = { class: string, for: string }
let label: Label | null = Label{ class: "hint", for: "email" }
label?.class // "hint"
label?.for // "email"Map keys
Before : in a map literal, a keyword is a key. The key of null: is the string "null":
let flags = Map{ class: "wide", null: "empty", true: "yes" }
flags.keys() // ["class", "null", "true"]
flags["null"] // "empty"Destructuring
A record destructuring takes a field named by a keyword when it binds the field to another name:
type Event = { type: string, target: string }
let event = Event{ type: "submit", target: "form" }
let { type: kind, target } = event
"${kind} on ${target}" // "submit on form"type as a declaration and as an expression
The token after type decides what it begins. A name begins a type declaration, ( begins a type expression:
type Point = { x: int, y: int }
type(Point) // Pointtest outside a test file
Outside a test file, test is a plain name:
let test = "ab" + "c"
test.length() // 3Applications
Records that mirror JSON
Data from JavaScript often has fields named type or class. A record type spells them as they are:
type Message = { type: string, text: string }
let messages = [
Message{ type: "info", text: "saved" },
Message{ type: "error", text: "disk full" },
Message{ type: "error", text: "timeout" },
]
let errors = messages | Array.filter({ x.type == "error" }) | Array.map({ x.text })
println(errors | String.join(", "))disk full, timeoutPossible issues
Destructuring shorthand
The shorthand { type } would bind a variable named type, which cannot exist:
type Event = { type: string }
let event = Event{ type: "click" }
let { type } = event // error: 'type' is a keyword and cannot be bound: write { type: name }Name the variable: let { type: kind } = event.
Map shorthand
Map{ type } would read a variable named type. Write the key and the value:
let kind = "click"
let event = Map{ type } // error: 'type' is a keyword: as a key it needs ':'Map{ type: kind } builds the map.
Parameter names
A keyword cannot name a parameter, so it cannot name a named argument either:
func render(type: string): string { "<${type}>" }
// error: 'type' is a keyword and cannot be a parameter namePick another word, such as kind or tag.
JavaScript habits
const, function, as, and undefined are reserved, so each is an error where JavaScript would accept it:
const limit = 10 // error: 'const' is a reserved wordlet n = 1
let count = n as int // error: 'as' is a reserved wordlet missing = undefined // error: 'undefined' is a reserved wordAnt writes let for const, func for function, and null for undefined.
Self outside an interface
let x: Self = 1 // error: 'Self' is a reserved wordPrelude names
Array, Map, println, panic, and the other standard library names are ordinary identifiers, not keywords. The prelude binds them, and no declaration may take one:
let println = "hi" // error: println is a prelude name and cannot be redeclaredSee also
Identifiers, type, interface, Self, test, Map literals, Record literals, null
