You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We often have longer enum type names and omitting them leads to much cleaner looking and more legible code.
Consider that instead of:
enum CompassPoint { north, east, south, west }
let direction = CompassPoint.north
switch(direction as CompassPoint) {
case CompassPoint.north:
console.log("Lots of planets have a north")
break;
case CompassPoint.east:
console.log("Watch out for penguins")
break
case CompassPoint.south:
console.log("Where the sun rises")
break
case CompassPoint.west:
console.log("Where the skies are blue")
break
}
we can write:
enum CompassPoint { north, east, south, west }
let direction = CompassPoint.north
switch(direction as CompassPoint) {
case .north:
console.log("Lots of planets have a north")
break;
case .east:
console.log("Watch out for penguins")
break
case .south:
console.log("Where the sun rises")
break
case .west:
console.log("Where the skies are blue")
break
}
Even a simple assignment looks much nicer this way. Instead of
planet = isBlue ? Planet.earth : Planet.mars
we can have
planet = isBlue ? .earth : .mars
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
enum type inference
enum inference
infer enum type from value
short form enum values
enum case inference
enum without type name
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Swift allows for omitting the type name of the enum after the variable has already been declared as the enum's type.
For example...
... can be used as:
instead of
π Motivating Example
We often have longer enum type names and omitting them leads to much cleaner looking and more legible code.
Consider that instead of:
we can write:
Even a simple assignment looks much nicer this way. Instead of
we can have
The text was updated successfully, but these errors were encountered: