[swift-evolution] Will these two features be included in Swift 3?

Slava Pestov spestov at apple.com
Mon Dec 7 13:06:05 CST 2015


> On Dec 7, 2015, at 4:56 AM, Krzysztof Siejkowski via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Oh, it could be so, I’m just not aware of it. I’ve tried Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c) with
> 
> extension Array where Element == Double { 
>      func foo() -> String { 
>          return "hello doubles!" 
>      } 
> }
> 
> and got
> 
> repl.swift:1:31: error: same-type requirement makes generic parameter 'Element' non-generic
> extension Array where Element == Double {
>                               ^

This is an artificial limitation.

In the case of a generic signature of a function, you really *do* want to prevent the user from constraining parameters to the point of becoming non-generic, eg the following does not make sense:

func foo<T where T == Int>(t: T) // why not just say func foo(t: Int) instead?

However the same diagnostic logic is used for validating generic signatures of constrained extensions, when in fact it needs a slightly different set of checks.

I think this is something that a community member could figure out and fix pretty easily without going through the evolution process -- its really a language change at this point.

Slava


> 
> Could you please point to docs / example / reference?
> 
> All the best,
> Krzysztof
> 
> 
> On 7 December 2015 at 13:34:51, Davide De Franceschi via swift-evolution (swift-evolution at swift.org <mailto:swift-evolution at swift.org>) wrote:
> 
>> I thought constraining an extension to a single type (#1) was already in 2.2?
>> I've seen it in a SPM example and also someone confirmed to me that it compiled
>> 
>> It went like
>> extension Array where Element == Double { // add stuff }
>> 
>>> On 7 Dec 2015, at 12:14, Krzysztof Siejkowski via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Concerning extension constraining, it’s already doable with:
>>> 
>>> ```
>>> protocol DoubleProtocol {}
>>> 
>>> extension Double : DoubleProtocol {}
>>> 
>>> extension Array where Element : DoubleProtocol {
>>>     func onlyForDoubles() -> String {
>>>             return "hello doubles!"
>>>     }
>>> }
>>> 
>>> [1.2].onlyForDoubles() // „hello doubles!”
>>> ["a"].onlyForDoubles() // error: type of expression is ambiguous without more context
>>> ```
>>> 
>>> However, I personally like the idea of making a syntactic sugar for that case.
>>> 
>>> All the best,
>>> Krzysztof
>>> 
>>> 
>>> On 7 December 2015 at 13:01:11, Krzysztof Siejkowski (krzysztof at siejkowski.net <mailto:krzysztof at siejkowski.net>) wrote:
>>> 
>>>> Concerning generic typealiases, the topic is already being discussed in „Generic `typealias`s” thread: https://lists.swift.org/pipermail/swift-evolution/2015-December/000132.html <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_pipermail_swift-2Devolution_2015-2DDecember_000132.html&d=BQMFaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=Uu9iNLnY0h1pMgusxPvGAdQh7wn-fCLNd0vIsJCqWtk&m=Uzu4AUxAPExJgD7SwlP-qUxaoP5S2zFxkNZqp8nUDN0&s=egkhmUc4MUnSb_VqledTCNrMEoPiGFA5RpGrmDsx5Sc&e=>. The core Swift team approves:
>>>> 
>>>> > Yes, this is definitely something that I (at least) would
>>>> like to see. Patches welcome :-)
>>>> > Chris (Lattner)
>>>> 
>>>> All the best,
>>>> Krzysztof
>>>> 
>>>> On 7 December 2015 at 12:41:05, Tuur Anton via swift-evolution (swift-evolution at swift.org <mailto:swift-evolution at swift.org>) wrote:
>>>> 
>>>>> Can you please add these features in Swift 3?
>>>>> 
>>>>> 1. The ability to do this:
>>>>> extension Array<Double> {
>>>>>     //extend arrays of doubles
>>>>> }
>>>>> 
>>>>> 2. Generic typealiases:
>>>>> struct Foo<T,V> {
>>>>>     let t: T
>>>>>     let v: V
>>>>> }
>>>>> typealias IntFoo<V> = Foo<Int,V> //Error in Swift 2.1
>>>>>  _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=BQMFaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=Uu9iNLnY0h1pMgusxPvGAdQh7wn-fCLNd0vIsJCqWtk&m=Uzu4AUxAPExJgD7SwlP-qUxaoP5S2zFxkNZqp8nUDN0&s=DDvpZ11cAh4tJzwSrC_Znf-_EuEh9_qzIeYp3oCdOyg&e=>
>>>  _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=BQMFaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=Uu9iNLnY0h1pMgusxPvGAdQh7wn-fCLNd0vIsJCqWtk&m=Uzu4AUxAPExJgD7SwlP-qUxaoP5S2zFxkNZqp8nUDN0&s=DDvpZ11cAh4tJzwSrC_Znf-_EuEh9_qzIeYp3oCdOyg&e=>
>> 
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=BQIGaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=Uu9iNLnY0h1pMgusxPvGAdQh7wn-fCLNd0vIsJCqWtk&m=Uzu4AUxAPExJgD7SwlP-qUxaoP5S2zFxkNZqp8nUDN0&s=DDvpZ11cAh4tJzwSrC_Znf-_EuEh9_qzIeYp3oCdOyg&e= <https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.swift.org_mailman_listinfo_swift-2Devolution&d=BQIGaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=Uu9iNLnY0h1pMgusxPvGAdQh7wn-fCLNd0vIsJCqWtk&m=Uzu4AUxAPExJgD7SwlP-qUxaoP5S2zFxkNZqp8nUDN0&s=DDvpZ11cAh4tJzwSrC_Znf-_EuEh9_qzIeYp3oCdOyg&e=> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/a7ce3c69/attachment.html>


More information about the swift-evolution mailing list