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

Pete Vilter vilterp at uchicago.edu
Sat Mar 14 18:51:44 CDT 2015


Hi Tim,
This would be very nice to have in Swift/K — is there a proposal to add it?
The syntax is similar to Java static methods; should be familiar to people
coming from that background. One question (which applies to Java as well):
why is it necessary to declare the type variables at the beginning of the
line? Can't the compiler just see what variables are being used in the
return and argument types?
Thanks,
—Pete

On Fri, Mar 13, 2015 at 2:44 PM Tim Armstrong <tim.g.armstrong at gmail.com>
wrote:

> 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
> _______________________________________________
> Swift-devel mailing list
> Swift-devel at ci.uchicago.edu
> https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-devel/attachments/20150314/5d70d03a/attachment.html>


More information about the Swift-devel mailing list