[swift-evolution] [swift-evolution-announce] [Review] Replace `typealias` keyword with `associatedtype` for associated type declarations

Drew Crawford drew at sealedabstract.com
Sun Jan 3 04:46:09 CST 2016


> 
> 	* What is your evaluation of the proposal?

+1

> 	* Is the problem being addressed significant enough to warrant a change to Swift?

Yes.  A typealias in a protocol and a typealias anywhere else are 2 very different things.

* One is almost a preprocessor macro
* The other basically defines the protocol as a generic type, which has a lot of strange follow-on consequences

There are plenty <http://stackoverflow.com/questions/27725803/how-to-use-generic-protocol-as-a-variable-type> of questions <https://groups.google.com/forum/#!topic/swift-language/jzElQYwe9sM> online related to this confusion.

In addition the change is trivial and code could be transitioned automatically.

> 	* Does this proposal fit well with the feel and direction of Swift?

The choice of keyword "associatedtype" is already used in a common compiler error message:

> protocol 'Printable' can only be used as a generic constraint because it has Self or associated type requirements


Using "associatedtype" here is consistent with that error message and makes it more understandable for new users.

> 	* If you have you used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

I am an occasional user of Rust; Rust uses the same keyword ("type") in both of these cases.  IMO that choice is suffers from the same problems in Rust that it does here.

> 	* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?


One "potential" problem with this proposal is that it technically forbids the use of a "real" typealias in a protocol e.g.

protocol Foo {
    typealias F = Int
}

is now illegal.

To evaluate the severity of this problem I checked a private codebase with 47 usages of typealias.  One usage of the 47 would be illegal:

protocol Foo {
	  #if arch(x86_64) || arch(arm64)
                typealias secondstype = Int64
                typealias usecstype = Int64
          #else
                typealias secondstype = __darwin_time_t
                typealias usecstype = __darwin_suseconds_t
           #endif
	func setTimeout(s: secondstype, u: usecstype) throws
}

I refactored this to move the typealiases to top level.  That is not ideal, but I think it is outweighed by the advantages of this proposal.

While auditing this codebase for illegal typealiases I did find a comment that was quite confused about the difference between typealias and associatedtype.  So that convinces me even more about the importance of this proposal.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160103/dc6fa5c1/attachment.html>


More information about the swift-evolution mailing list