What about the parsing issues of bare last statement? Unlike in SwiftUI @ViewBuilder contexts, in the contexts we’re discussing here the return type will very often be concrete, meaning that leading-dot syntax will be used often, but with no separating (non-whitespace) character or keyword between multiple expressions this will become ambiguous.
Imagine we start with this code:
var color: Color {
if isSelected {
.purple
} else {
.gray
}
}
We want to add a print statement to the else
branch, and the options under consideration so far are:
- A keyword (e.g.,
then
) - An operator (e.g.,
<-
) - No separator
- Semicolons
A keyword
var color: Color {
if isSelected {
.purple
} else {
print("Not selected")
// Leading-dot syntax still works, but we had
// to modify the return value using the keyword,
// and also there are concerns about parsing a new keyword.
then .gray
}
}
An operator
var color: Color {
if isSelected {
.purple
} else {
print("Not selected")
// Leading-dot syntax still works, but we had
// to modify the return value using the operator.
// At least we avoid issues related to parsing a new keyword.
<- .gray
}
}
No separator
var color: Color {
if isSelected {
.purple
} else {
print("Not selected")
// Leading-dot syntax becomes ambiguous, so
// we still end up having to modify
// the line of the return value.
Color.gray
}
}
Semicolons
var color: Color {
if isSelected {
.purple
} else {
print("Not selected");
// The only option that allows me to call `print(_:)`
// without having to also modify the preexisting line.
.gray
}
}