[swift-evolution] [Pitch] Moving where Clauses Out Of Parameter Lists

plx plxswift at icloud.com
Wed Apr 6 14:41:06 CDT 2016


I don’t see much value in moving them out of the angle brackets but preserving the position; consider:

  // status-quo:
  func anyCommonElements <T: SequenceType, U: SequenceType
    where 
    T.Generator.Element: Equatable, 
    T.Generator.Element == U.Generator.Element> 
    (lhs: T, _ rhs: U) -> Bool
  
  // as-per proposal:
  func anyCommonElements <T: SequenceType, U: SequenceType>
    where 
    T.Generator.Element: Equatable, 
    T.Generator.Element == U.Generator.Element
    (lhs: T, _ rhs: U) -> Bool

…not very different! Or, if you want it with fewer lines:

  // status-quo:
  func anyCommonElements <T: SequenceType, U: SequenceType
    where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> 
    (lhs: T, _ rhs: U) -> Bool
  
  // as-per proposal:
  func anyCommonElements <T: SequenceType, U: SequenceType>
    where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element
    (lhs: T, _ rhs: U) -> Bool

…still not very different!

Moving the `where` to come after “everything else" seems like an improvement for functions:

  func anyCommonElements <T: SequenceType, U: SequenceType>(lhs: T, _ rhs: U) -> Bool
    where 
    T.Generator.Element: Equatable, 
    T.Generator.Element == U.Generator.Element { 
  // body here...
}

…and presumably-also for type declarations:

  class SomeClass<A,B,C,D,E> : BaseClass, SomeProtocol, AnotherProtocol
    where
    A.Something == SomethingElse,
    B.Input == C.Output {

  }

..,but would take some getting-used-to.

Losing the ability to write `:` constraints inline in the brackets seems a non-starter, but perhaps constraints within the brackets could be limited to `:` constraints?

> On Apr 6, 2016, at 1:30 PM, Developer via swift-evolution <swift-evolution at swift.org> wrote:
> 
> If you've ever gotten to the point where you have a sufficiently generic interface to a thing and you need to constrain it, possibly in an extension, maybe for a generic free function or operator, you know what a pain the syntax can be for these kinds of operations.  For example, the Swift book implements this example to motivate where clauses
> 
> func anyCommonElements <T: SequenceType, U: SequenceType where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, _ rhs: U) -> Bool
> 
> This is noisy and uncomfortable to my eyes, and almost impossible to align correctly.  Per a short discussion on Twitter with Joe Groff and Erica Sadun, I'd like so see what the community feels about moving the where clause out of the angle brackets.  So that example becomes
> 
> func anyCommonElements <T: SequenceType, U: SequenceType>
> where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element
> (lhs: T, _ rhs: U) -> Bool
> 
> Or, if you're feeling ambitious, even
> 
> func anyCommonElements <T, U>
> where T : SequenceType, U : SequenceType,
> T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element
> (lhs: T, _ rhs: U) -> Bool
> 
> Thoughts?
> 
> ~Robert Widmann
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list