Skip to content

Instantly share code, notes, and snippets.

@wokalski
Last active March 12, 2016 19:16
Show Gist options
  • Save wokalski/580745266cdb09806084 to your computer and use it in GitHub Desktop.
Save wokalski/580745266cdb09806084 to your computer and use it in GitHub Desktop.
Patch proposal (idea validation)

I suggest the following (minor(?)) extension of typealias capabilities:

typealias ExampleAlias = CollectionType where Generator.Element : Equatable, Index : SignedIntegerType

Rationale:

  1. Lengthy constraints in generic functions:
func foo<T: CollectionType where T.Generator.Element : Equatable, T.Index : SignedIntegerType>(a a: T, b: T)

This is really disturbing when reading source code. One could argue that it's actually good because it describes the function, but then:

  1. it allows to abstract constraints to reuse them in code

According to @jckarter it has been covered in the generics manifesto in Swift Evolution, but I couldn't find exactly same thing.

Indeed a similar idea has been proposed but with one significant difference - contributors suggested constraints of non-protocol type, which is not even supported generic constraints in general, yet.

e.g.

func foo<T: CollectionType where T.Generator.Element : Equatable, T.Index : Int>(a a: T, b: T)

will fail to compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment