Can you access a switch statement variable anonymously from a case statement?

For example, say I have this switch statement:

let foo = bar()
switch foo {
case foo0: print(foo.description)
case foo1: print(foo.name)
...

Could it be abbreviated so that I don't have to declare the "let"?

switch bar() {
case foo0: print($0.description)
case foo1: print($0.name)
...

Answered by DTS Engineer in 659601022

Could it be abbreviated so that I don't have to declare the let?

Not in the language as its currently designed. You have a couple of choices here:
  • If you’d like to explore better solutions within the existing language, the best place for that is Swift Forums > Using Swift. I’m happy to talk Swift here but you’ll get more traction over there.

  • If you’d like the language to change to support this feature, you can raise it over in Swift Forums > Evolution.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
You should probably post this in a Swift forum. It doesn't have anything to do with Foundation.
Accepted Answer

Could it be abbreviated so that I don't have to declare the let?

Not in the language as its currently designed. You have a couple of choices here:
  • If you’d like to explore better solutions within the existing language, the best place for that is Swift Forums > Using Swift. I’m happy to talk Swift here but you’ll get more traction over there.

  • If you’d like the language to change to support this feature, you can raise it over in Swift Forums > Evolution.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
In this case, would you need to retain the instance of Foo after switch is completed ?
You would also need to declare switch let or switch var.

I'm not convinced of the added value.
Can you access a switch statement variable anonymously from a case statement?
 
 
Q