Am I missing something on why a key path can't be used here?

Have a helper method:

extension MutableCollection {
  fileprivate mutating func overwrite<I: IteratorProtocol>(
    prefixUsing source: inout I,
    _ transform: (I.Element) -> Element
  ) -> Index {
    var current = startIndex
    let end = endIndex
    while current < end, let seed = source.next() {
      self[current] = transform(seed)
      formIndex(after: &current)
    }
    return current
  }
}

Which is used by:

extension MutableCollection {
  @discardableResult
  public mutating func overwrite<I: IteratorProtocol>(
    prefixUsing source: inout I
  ) -> Index where I.Element == Element {
    return overwrite(prefixUsing: &source, { $0 })
  }
}

Instead of "{ $0 }," I wanted to use "\Element.self," but the compiler said there was no conversion between the (writable) key path and an (Element) -> Element function. Using the latest Xcode. Isn't this supposed to work? (I added an "as KeyPath" once, and it made no difference, so the problem isn't the mutability.)

It's probably this bug:

1 Like
Terms of Service

Privacy Policy

Cookie Policy