[Swift-devel] Polymorphic type notation in Swift/T

Tim Armstrong tim.g.armstrong at gmail.com
Fri Mar 13 14:44:24 CDT 2015


Here's how I define contains in Swift/T:

<K, V> (boolean o) contains(V A[K], K key)

K and V declares that K and V are type variables in the scope of the
function definition.  I.e. when typechecking call to the function, K and V
can be any type, so long as they match in all places.

The algorithm for typechecking is to match up input types to the argument
types and bind the type variables, e.g. matching a string[int] array to
V[K] would bind V=string and K=int, and matching int to K  would bind
K=int.  If there isn't a consistent way to bind K and V, e.g. if one of the
K's was a string, then typechecking fails.


For split, the function type would look like:

<T, K> T[int][K] split(T[K], int n)

- Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-devel/attachments/20150313/a8e6a077/attachment.html>


More information about the Swift-devel mailing list