[Pitch] Elide `some` in Swift 6

is and as carry a bunch of added overhead because they have to check subclassing, bridging, collection/Optional subtyping, and all the other various dynamic casting edge cases. @_specialize can do exact type checks in a way that's probably more efficient. That added overhead is mostly independently of whether existentials are involved or not, though.

Ultimately, though, I'd like to see us expose that kind of more expressive efficient type testing in the language too. It would be great if you could do things like

func foo<T>(x: T, y: T) -> T {
  if where T == Int {
    return x + y // we can assume x and y are Int here
  }

  if <U> where T == Array<U> {
    return x + y // we can assume x and y are some kind of Array here
  }

  if where T: Addable {
    return x.add(y) // we can assume x and y conform to Addable here
  }

  return x
}

without the overhead of the extraneous checks, the need for a value of a type to ask for properties of the type itself, or the implied wrapping of casting something as? P.

14 Likes