Line endings
A newline ends a statement in Ant, as a semicolon does. A few clear signs continue a statement onto the next line: an operator or an open bracket at the end of a line, or ., ?., |, or else at the start of the next one.
Details
- A line feed
\n, a carriage return and line feed\r\n, and a carriage return\ralone are each one line break. A file saved with any of them means the same program. - A line break inside a string or a raw string is one
\nin the value, whatever the file uses. - A statement continues when its line ends with a token that cannot end an expression, such as a binary operator,
,,(,[,{, or=, or when the next non-blank line starts with.,?.,|, orelse. Nothing else continues it. - Inside parentheses, square brackets, and braces that are not a block, newlines are ignored.
- A
returnat the end of a line returnsvoid, even when the next line starts with.,?.,|, orelse. - A trailing comment does not change where a statement ends.
Basic examples
Each line is a statement:
let width = 3
let height = 4
println(width * height)12An operator at the end of a line continues the expression on the next:
let total = 120 +
45 +
7
println(total)172A pipe at the start of a line continues the expression above:
let names = ["ann", "bob", "cy"]
let line = names
| Array.map({ x.toUpper() })
| String.join(", ")
println(line)ANN, BOB, CYScope
Semicolons
A semicolon ends a statement too, so two statements can share a line:
let n = 5
if n > 3 { print("big"); println("!") }big!Dot calls on new lines
A line that starts with . or ?. continues the line above, so a chain of dot calls can put one call on each line:
let title = " line endings "
.trim()
.toUpper()
println(title)
type User = { name: string }
let user: User | null = null
let name = user
?.name
println(name)LINE ENDINGS
nullelse on the next line
A line that starts with else continues the if above it:
let n = 5
let size = if n < 3 { "small" }
else { "large" }
println(size)largeInside brackets
Inside parentheses, square brackets, and the braces of a literal, newlines are ignored. Items are separated by commas, and a trailing comma is allowed:
type Point = { x: int, y: int }
let primes = [
2,
3,
5,
]
let origin = Point{
x: 0,
y: 0,
}
println(primes)
println(origin)
println(Math.max(
primes.length(),
origin.x,
))[2, 3, 5]
Point{ x: 0, y: 0 }
3The braces of a block are different: there, a newline separates the statements.
Union types
A | at the start of a line continues a union type too:
type Circle = { radius: float64 }
type Square = { side: float64 }
type Triangle = { base: float64, height: float64 }
type Shape = Circle
| Square
| Triangle
let shape: Shape = Square{ side: 2.0 }
println(shape)Square{ side: 2.0 }Strings across lines
A string or raw string literal may span lines. Each line break in it is one \n in the value:
"roses
are red" // "roses\nare red"
"a
b" == "a\nb" // true
`a
b` == "a\nb" // trueTrailing comments
A comment at the end of a line does not change how the line ends. The + still continues the expression:
let price = 100 + // base
20
println(price)120Possible issues
A leading operator starts a new statement
Only ., ?., |, and else continue a line from the start of the next one. A - there is a unary minus in a statement of its own, so total below is 10:
let total = 10
-3
println(total)10Other binary operators cannot start a statement, so the line is a syntax error:
let ready = true
&& false // error: unexpected '&&'Put the operator at the end of the line instead:
let ready = true &&
false
println(ready)falseA block's { on the next line
The { of a block must sit on the same line as its if, while, or for header. A newline ends the header expression, even inside parentheses:
let n = 5
if n > 3 // error: expected '{' after the if header, got newline
{ println("big") }Put the { on the same line as the header: if n > 3 {.
A line that starts with ( or [
A line that starts with ( or [ begins a new statement. It never continues a call or an index on the line above:
let xs = [10, 20]
let first = xs
[0]
println(first)[10, 20]Keep the [0] or the ( of a call on the same line as what it applies to: xs[0].
return at the end of a line
A return at the end of a line returns void. The next line is a statement of its own:
func double(n: int): int {
return // error: cannot use void where int is expected
n * 2
}A line that starts with . does not continue a bare return either. No statement starts with ., so that line is a syntax error, and the bare return fails as in the example above:
func shout(s: string): string {
return // error: cannot use void where string is expected
.toUpper() // error: unexpected '.'
}Start the value on the same line as return.
A regular expression on one line
A regular expression literal cannot hold a line break:
let pattern = /one
// error: unterminated regular expression
two/Write \n in the pattern to match a line break.
