[swift-evolution] [Proposal] Disallow redundant `Any<...>` constructs

Adrian Zubarev adrian.zubarev at devandartist.com
Fri May 20 09:53:20 CDT 2016


What I meant here is, that the example should produce an error because it is basically `Any<Any<Any<…>>>` which should be banned. It depends on the context. Using typealias in general can be allowed but it depends how you will use and nest it.

This example is valid:

protocol A {} protocol B {} protocol C {}

typealias AB = Any<A, B>
typealias ABC = Any<AB, C>

If we ban `Any<Any<...>>` one should not abuse `typealias` to achieve that, even if its useless. I’m not sure how hard that would be implement that restriction.

The example you was pointing at wouldn’t work anyway because the rule #3 and rule #4 should raise an error. Thats how I see it.

If I’m not correct please provide an example that makes more sense. :)

-- 
Adrian Zubarev
Sent with Airmail

Am 20. Mai 2016 bei 16:44:35, Matthew Johnson (matthew at anandabits.com) schrieb:



Sent from my iPad

On May 20, 2016, at 4:39 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:

This is a follow up proposal to SE-0095 which should be considered for Swift 3 if SE-0095 will be accepted.

Here is the formatted draft: https://github.com/DevAndArtist/swift-evolution/blob/master/proposals/nnnn-ban-redundancy-in-any-existential.md

Please provide your feedback in this thread, and don’t make a race who is making a better proposal on the exact same topic.

If you spot any types or other mistakes I’d be happy to see you pointing me to them. ;)

-- 
Adrian Zubarev
Sent with Airmail
Disallow redundant Any<...> constructs

Proposal: SE-NNNN
Author: Adrian Zubarev
Status: Awaiting review
Review manager: TBD
Introduction

This is a follow up proposal to SE–0095, if it will be accepted for Swift 3. The current concept of Any<...> introduced in SE–0095 will allow creation of redundant types like Any<A> == A. I propose to disallow such redundancy in Swift 3 to prevent breaking changes in a future version of Swift.

Swift-evolution thread: [Proposal] Disallow redundant Any<...> constructs

Motivation

If SE–0095 will be accepted there will be future proposals to enhance its capabilities. Two of these will be Any-type requirement (where type could be class, struct or enum) and Class requirement. Without any restrictions these will introduce more redundancy.

As said before it is possible to create redundant types like Any<A> == A or endless shadowed redundant nesting:

typealias A_1 = Any<A>
typealias A_2 = Any<A_1>
typealias A_3 = Any<A_2>
/* and so on */

This proposal should ban redundancy right from the beginning. If there might be any desire to relax a few things, it won’t introduce any breaking changes for Any<...> existential.

Why do you think having multiple typealiases for the same type is a problem?  The whole point of typealias is to alias the name of the type.  


Proposed solution

If empty Any<> won’t be disallowed in SE–0095, we should disallow nesting empty Any<> inside of Any<...>.

Disallow nesting Any (type refers to current typealias Any = protocol<>) inside of Any<...>.

Disallow Any<...> containing a single Type like Any<Type>.

The first three rules will ban constructs like Any<Any<>, Type> or Any<Any, Type> and force the developer to use Type instead.

Disallow nesting a single Any<...> inside another Any<...>.
e.g. Any<Any<FirstType, SecondType>>
Disallow same type usage like Any<A, A> or Any<A, B, A> and force the developer to use A or Any<A, B> if A and B are distinct.

Disallow forming redundant types when the provided constraints are not independent.

// Right now `type` can only be `protocol` but in the future Any<...>   
// could also allow `class`, `struct` and `enum`.
// In this example `B` and `C` are distinct.
type A: B, C {}   
      
// all following types are equivalent to `A`
Any<A, Any<B, C>>
Any<Any<A, B>, C>
Any<Any<A, C>, B>
Any<A, B, C>
Any<A, B>
Any<A, C>

If all contraints form a known Type provide a Fix-it error depending on the current context. If there is more than one Type, provide all alternatives to the developer.

Using Any<...> in a generic context might not produce a Fix-it error:

protocol A {}
protocol B {}
protocol C: A, B {}
          
// there is no need for `Fix-it` in such a context
func foo<T: Any<A, B>>(value: T) {}

Impact on existing code

These changes will break existing code. Projects abusing Any<...> to create redundant types should be reconsidered of usings the equivalent Type the compiler would infer. One would be forced to use A instead of Any<A> for example. A Fix-it error message can help the developer to migrate his project.

Alternatives considered

Leave redundancy as-is for Swift 3 and live with it.
Deprecate redundancy in a future version of Swift, which will introduce breaking changes.
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160520/0367b791/attachment.html>


More information about the swift-evolution mailing list