Uploaded image for project: 'Swift'
  1. Swift
  2. SR-8109

Swift 4.2 Regression: Single-element tuple not prevented

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      Xcode 10 beta 1, Xcode 10 beta 2, recent dev snapshots

      Description

      Related thread (which I wish got some clarifications from the core team): https://forums.swift.org/t/roadmap-for-and-state-of-single-element-tuple-types/13711

      As far as I understand, single element tuples should be prevented in Swift.

      The following program does not compile with (the default toolchain of) Xcode 9.4, as expected.

      BUT with recent versions of the compiler (default toolchains of Xcode 10 beta 1 and beta 2, as well as recent dev snapshots), it does compile.

      let t = (label: 123)
      print(t.label + t.label) // prints 246
      

      So we can define `t` to be of an instance of a (labeled) single-element tuple type, and use it as such.

      (
      But we cannot dump or print or otherwise access its type without getting (nonsensical) errors, and we cannot define its type explicitly, meaning the following will not compile:

      let t: (label: Int) = (label: 123)
      print(t.label + t.label)
      

      )

      Also, as a side note, the following program compiles with all versions of the compiler (including Xcode 9.4):

      let r = (label: 12).label + (label: 34).label
      print(r) // prints 46
      

      But I guess it shouldn't?

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jens Jens Persson
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: