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

Pete Vilter vilterp at uchicago.edu
Sun Mar 15 14:01:07 CDT 2015


Thanks Tim, that reasoning makes sense.

On Sun, Mar 15, 2015 at 1:16 PM Tim Armstrong <tim.g.armstrong at gmail.com>
wrote:

> This is only currently supported for external Tcl functions, btw, not
> regular Swift composite functions.
>
> It's possible to do what you suggest - assume that any unknown types in
> the type signature are type variables.  It would be pretty straightforward
> to implement too.  The downside is that can lead to surprising behaviour
> from the user's perspective.
>
> For example, suppose I declare a function with this type:
>
>   f(Int x)
>
> Int isn't the name of a type (the real integer type is int), so it would
> be inferred to be a type variable, and then you have a function that looks
> like it accepts ints, but actually accepts any input argument.
>
> There are other scenarios too - e.g. if a type is defined in an imported
> module but you forget to import the module, suddenly you have a type
> variable where you thought you had a regular type.
>
> I guess the way to think about it is that you're asking the programmer to
> add a little redundant information about their intent that then allows the
> compiler to check that their code is doing what they intended.
>
> - Tim
>
> On 14 March 2015 at 18:51, Pete Vilter <vilterp at uchicago.edu> wrote:
>
>> 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/20150315/a5f7ec90/attachment.html>


More information about the Swift-devel mailing list