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

KeyPath cannot infer type in [keyPath:] context

    XMLWordPrintable

    Details

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

      Swift 4.1

      Description

      This code does not compile:

      "str"[keyPath: \.count]
      

      It gives the error “Type of expression is ambiguous without more context.” Adding an explicit type to the left of the dot fixes it:

      "str"[keyPath: \String.count]
      

      However, with a trivial wrapping of the `[keyPath: …]` access in a func, the implicit type works:

      func applyKeyPath<T,U>(_ keyPath: KeyPath<T,U>, to val: T) -> U {
          return val[keyPath: keyPath]
      }
      applyKeyPath(\.count, to: "str")
      

      Is this a bug?

      May be related to SR-6740, but seems distinct.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mdiep Matt Diephouse
                Reporter:
                pcantrell Paul Cantrell
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: