letâs suppose we all lived in an alternate reality called Earth-159 where we all used a programming language called Swoft!
Swoft, is a lot like Swift, except in Swoft, instead of typing out the word return, you simply type a single backslash character (\) before whatever expression you wanted to return.
so instead of writing
func makeBreakfast(from ingredients:[Ingredient]) -> Ingredient?
{
return ingredients.first
}
you would write
func makeBreakfast(from ingredients:[Ingredient]) -> Ingredient?
{
\ ingredients.first
}
to us in the Real World, this easy to miss, awful for readability, and it doesnât take long to image how a Junior Dev could trip on a newline set themselves on fire and accidentally demolish your entire Company trying to edit this.
but to the denizens of Earth-159, they are used to this, have experience using the feature, and to their eyes, the \ token stands out a lot more to them than it does to us. so to them, this code is very readable, in fact spelling out the whole word return just feels gratuitous.
now we are from the Superior Earth and this strange language Swoft is just evidence of how backwards and primitive Earth-159 is! but Swift developers do a lot of things that appear dimwitted to Swoft developers too. for example, Swift developers tend to zone out and write
func makeBreakfast(from ingredients:[Ingredient]) -> Ingredient?
{
ingredients.first
}
which is rejected by the Swoft compiler because Void is not compatible with Ingredient?.
you see, on Earth-159, the implicit single return statement was never invented at all, so while Swift developers happily spin out items.map { ($0.id, $0.name) }, Swoft developers are still typing the entire mouthful that is items.map { \ ($0.id, $0.name) }.
itâs not that the idea never occurred to them, itâs just that going from \ to just doesnât save enough textual overhead for implicit single return to be worth it. if fact, if this were pitched on Swoft Evolution, it would probably accumulate many -1âs from Swoft users who think it is just making the language more confusing for no good reason.
now Swoft is not a Real Language, but Rust is, and Rust really does have a real life \ keyword.
in Rust, the equivalent of \ is . that is, for Rust users, it is the absence of the ; character that signifies that an expression is to be returned.
this is really fundamentally different from what is being proposed to be added to Swift. to understand that an expression is being returned in Rust, you just need to look at whether there is a semicolon at the end of the line. in Swift, you would have to visualize the entire control flow of the function.
for a single statement function, this was not a problem, because the control flow is trivial. but for multi-statement functions, this quickly becomes unreadable and difficult to skim.
with Swift, we made the decision long ago to not have semicolons, and we enjoy the conveniences that come with not having semicolons. but one of the future designs this forfeited was the ability to introduce any kind of multi-statement implicit return without dramatically harming readability.