[Pitch] Last expression as return value

I have a number of concerns.

What’s the plan for loops? What about if a control flow statement, like break or continue, is the last thing to be executed? If loops or control flow statements are banned, that means this proposal doesn’t get us to “literally everything is an expression”, so are we actually going to be happier with this resting place?

Suppose you write a function and forget to write the expression that computes the return value, so whatever expression happens to be last is interpreted as computing the return value. Currently, we can always diagnose that (except with a single-expression functions); with this change, we will only diagnose it if the last expression’s type happens to not match the function’s result type. Are we okay with no longer getting that hint from the compiler?

When that does happen, what’s the diagnostic going to look like? Will it be understandable to beginners, or will they get inscrutable type errors?

When that does happen, are we worried that the type checker will go exponential trying to make ill-fitting expressions work as return values?

To try to keep some ability to detect forgotten return values in these situations, should we require (or suggest with a warning) an explicit return statement for extremely permissive return types like existentials or Any specifically, or dubious return value expressions like Void expressions or @discardableResult functions, or at least the former combined with the latter? If so, what specifically should we exclude?

51 Likes