Swift Switch Statement Considered All Cases of Int, But Compiler Still Display Error

Swift switch statement considered all cases of Int, but compiler still display error

Update for Swift 3: Swift 3 introduced ClosedRange which makes
it possible to define a range like 1...Int.max including the
largest possible integer (compare Ranges in Swift 3). So this compiles and works as expected,
but still requires a default case to satisfy the compiler:

extension Int {
enum Kind {
case negative, zero, positive
}
var kind: Kind {
switch self {
case 0:
return .zero
case 1...Int.max:
return .positive
case Int.min...(-1):
return .negative
default:
fatalError("Oops, this should not happen")
}
}
}

There are other bug reports where the Swift compiler does not
correctly determine the exhaustiveness of switch-statements, such
as https://bugs.swift.org/browse/SR-766, where Apple engineer Joe Groff
commented:

Unfortunately, integer operations like '...' and '<' are just plain functions to Swift, so it'd be difficult to do this kind of analysis. Even with special case understanding of integer intervals, I think there are still cases in the full generality of pattern matching for which exhaustiveness matching would be undecidable. We may eventually be able to handle some cases, but there will always be special cases involved in doing so.


Old answer: The compiler is not so smart to recognize that you have covered
all possible cases. One possible solution is to add a default
case with a fatalError():

var kind: Kind {
switch self {
case 0:
return .Zero
case let x where x > 0:
return .Positive
case let x where x < 0:
return .Negative
default:
fatalError("Oops, this should not happen")
}
}

Or make case 0: the default case:

var kind: Kind {
switch self {
case let x where x > 0:
return .Positive
case let x where x < 0:
return .Negative
default:
return .Zero
}
}

(Remark: I initially thought that the following would work correctly
without needed a default case:

var kind: Kind {
switch self {
case 0:
return .Zero
case 1 ... Int.max:
return .Positive
case Int.min ... -1:
return .Negative
}
}

However, this compiles, but aborts at runtime because you cannot
create the range 1 ... Int.max. More information around this
problem can be found in the article Ranges and Intervals in Swift.)

Exhaustive condition of switch case in Swift

Swift only truly verifies that a switch block is exhaustive when working with enum types. Even a switching on Bool requires a default block in addition to true and false:

var b = true
switch b {
case true: println("true")
case false: println("false")
}
// error: switch must be exhaustive, consider adding a default clause

With an enum, however, the compiler is happy to only look at the two cases:

enum MyBool {
case True
case False
}

var b = MyBool.True
switch b {
case .True: println("true")
case .False: println("false")
}

If you need to include a default block for the compiler's sake but don't have anything for it to do, the break keyword comes in handy:

var b = true
switch b {
case true: println("true")
case false: println("false")
default: break
}

Switch on Any.Type

Xcode generates the following warning on the above case: "Cast from 'Any.Type' to unrelated Type 'Int' always fails" which hints at the correct way:

 let t: Any.Type = Int.self
switch t {
case is Int.Type:
print("Int")
default:
print("Other")
}

Can a swift switch be exhaustive for type Double without a default case?

No because only enum types can be exhaustively checked.

But in this case, the problem is even deeper. Even if Integers could be exhaustively checked, you still couldn't exhaustively check Double without a where clause. One of the options is .nan ("not a number"), which you're not considering. So you might think to just add that case:

case .nan:
yAxisMinimum = .nan

Not only won't this make it exhaustive, it won't even work the way you'd expect.

var minY = Double.nan

switch minY {
case -(Double.infinity)..<0.9:
yAxisMinimum = 0.0
// ...
case .nan:
yAxisMinimum = .nan
default:
yAxisMinimum = 0
}

yAxisMinimum // 0

Why? Because of this:

var minY = Double.nan
minY == .nan // false

NaN is unequal to everything, including NaN. So there's no way to include it directly in a switch statement. You have to use a where clause:

case _ where minY.isNaN:
yAxisMinimum = .nan

And that's definitely beyond the compiler's ability to validate.

Expected declaration on switch declaration

You can't write switch rank { inside the struct directly , it should be inside a function init or any other custom one

public struct beltRank {
var rank = 0
var belt = ""
init(rank:Int) {
// write switch here
}
}


Related Topics



Leave a reply



Submit