Recent change makes Swift 4 less safe.

The latest beta doesn’t allow you to append “()” to an enum name with no associated value in a switch case.

This makes Swift less safe because appending that “()” is an excellent way to indicate that you are not ignoring the associated value in the swift case because there isn’t any.

Suppose that you later add an associated value. Then the switch case with the “()” will give a compile error, which is better than silently introducing a bug into your code.

This is intended because () is confusing. If you need that back, simply add (Void).

For more information read this proposal:
https://github.com/apple/swift-evolution/blob/master/proposals/0155-normalize-enum-case-representation.md

···

--
Adrian Zubarev
Sent with Airmail

Am 28. Juli 2017 um 23:23:24, Amir Michail via swift-evolution (swift-evolution@swift.org(mailto:swift-evolution@swift.org)) schrieb:

The latest beta doesn’t allow you to append “()” to an enum name with no associated value in a switch case.

This makes Swift less safe because appending that “()” is an excellent way to indicate that you are not ignoring the associated value in the swift case because there isn’t any.

Suppose that you later add an associated value. Then the switch case with the “()” will give a compile error, which is better than silently introducing a bug into your code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don’t agree with your assertions that this makes Swift “less safe” - if anything the bug that this fixed was anything but. When performing a pattern match, we expect the pattern to match the “shape” of the declaration itself. If ‘()’ was not part of that declaration, then you have no reason to indicate this as part of a pattern. Consider a slightly more extreme case that was allowed before we fixed this bug (and, for compatibility reasons, is still allowed in Swift 3 mode)

enum E {
  case A, B, C, D
}

func testE(e: E) {
  switch e {
  case .A(let x): print(x)
  default: break
  }
}

Assuming this survives SILGen, which it often doesn’t, what do you feel a reasonable value for “x” should be? Our compiler thinks (thought?) ‘Void’.

~Robert Widmann

···

On Jul 28, 2017, at 2:23 PM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

The latest beta doesn’t allow you to append “()” to an enum name with no associated value in a switch case.

This makes Swift less safe because appending that “()” is an excellent way to indicate that you are not ignoring the associated value in the swift case because there isn’t any.

Suppose that you later add an associated value. Then the switch case with the “()” will give a compile error, which is better than silently introducing a bug into your code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Terms of Service

Privacy Policy

Cookie Policy