[Pitch] Last expression as return value

I'm seeing in some comments bits of the real underlying problem, that the pitch doesn't directly address: if, else, switch, do, and catch are just missing the capability that closures have, via return. That has always been true, but the problem became more noticeable with if and switch expressions.

We don't need a new keyword. break and continue are both available. I prefer the former but don't care much either way.

let width = switch scalar.value {
default:
  log("this is unexpected, investigate this")
  break 4
}
let foo: String = do {
  try bar()
} catch {
  print(error)
  break "Error \(error)"
}
let foo = if x.isOdd {
  print("x was odd")
  break x + 1
} else {
  x
}

For reference: This compiles.

var void = {
  switch 1 {
  default: return
  }
} ()

And this compiles:

void = switch 1 {
default: ()
}

But this doesn't.

void = switch 1 {
default: break
}

I think it should. These are the same:

return 
return ()

So these should be the same.

break 
break ()

And all other values but () should follow.

3 Likes