Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Powerful Constraints for Associated Types #284

Merged
merged 12 commits into from Sep 23, 2016

Conversation

hartbit
Copy link
Collaborator

@hartbit hartbit commented May 2, 2016

It has been discussed in multiple threads and here is the final proposal.

@hartbit
Copy link
Collaborator Author

hartbit commented May 22, 2016

@lattner @DougGregor I guess you guys must be quite busy, but is there something missing from the proposal holding it from being merged?

@DougGregor
Copy link
Member

We're a bit swamped now, which is why we're so behind on handling PRs. The proposal looks good to me as it is; I'll try to get things merged/numbered soon.

@beccadax
Copy link
Contributor

Could this be given a less vague (and preferably sentence-case, which is the most common convention) title, like "Permit where clauses to constrain associated types"? That would help people understand what the proposal actually does when it hopefully eventually appears in this list.

@hartbit
Copy link
Collaborator Author

hartbit commented May 25, 2016

@brentdax I have a hard time finding a title because "Permit where clauses to constrain associated types" is only one part of the story. There is also the new where clause on associated types themselves.

@beccadax
Copy link
Contributor

beccadax commented May 25, 2016

@hartbit I actually think the title I suggested covers both features. In both cases, you're applying a where clause to an associated type, and thus constraining the types that associated type may be set to; it's just a question of whether that associated type comes from your own protocol or from some other protocol you're conforming to.

But even if that particular title doesn't cover the entire feature, I think we can come up with something that does. And even if we can't come up with a descriptive title that covers everything, I think a descriptive title that covers a large portion of what's in the proposal is better than a vague title that covers everything. The goal is to make sure that it's easy to pick out this particular feature in proposal listings, subject lines, etc.; a descriptive partial title is better for that purpose.

@hartbit
Copy link
Collaborator Author

hartbit commented May 25, 2016

@brentdax How about "Extend where clauses to constrain associated types"?

@beccadax
Copy link
Contributor

@hartbit The problem is, they already do constrain associated types when they're used in generics. "Extend where clauses to constrain associated types in protocol declarations"? Still not technically correct, but at least more suggestive.

@hartbit
Copy link
Collaborator Author

hartbit commented May 27, 2016

@brentdax done :)

@DougGregor Hi Doug. I'm a bit confused. I understand if you guys are all very busy. But is there a reason that proposals like SE-0099 get written, merged, scheduled and started review under two days while others like this stay unmerged for a month? Is it because this proposal is out of scope for Swift 3? If that's the case, I'm more than happy to postpone it until August. I just feels weird when there is little feedback and the whole merge and schedule process feels very inconsistent.

@DougGregor
Copy link
Member

It's likely out-of-scope for Swift 3, because it will require a bit of work in the type checker. Also, we're trying to get the various syntactic changes into Swift 3 so we don't have these kinds of syntactic tweaks in Swift 4. This proposal is more on the additive side.

(Also: @lattner seems to have more time to review/merge proposals than I do, and generics stuff is more in my domain)

@hartbit
Copy link
Collaborator Author

hartbit commented May 28, 2016

Thanks for the explanation :)


## Motivation

Currently, associated type declarations can only express simple inheritance constraints and not the more sophisticated constraints available to generic types with the `where` clause. Some designs, including many in the Standard Library, require more powerful constraints for associated types to be truly elegant. For example, the `SequenceType` protocol can be declared as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please hard-wrap lines in markdown. Otherwise diffs start to become very hard to work with.
  2. s/can/could—if it were supported—/?

Copy link
Collaborator Author

@hartbit hartbit Sep 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2) Not entirely sure what you mean?

# Permit where clauses to constrain associated types

* Proposal: [SE-XXXX](https://github.com/apple/swift-evolution/blob/master/proposals/XXXX-associated-types-constraints.md)
* Author(s): [David Hart](http://github.com/hartbit), [Jacob Bandes-Storch](jtbandes@gmail.com), [Douglas Gregor](dgregor@apple.com)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those email address links don't work in Markdown. The template has also changed recently.

* Proposal: [SE-NNNN](NNNN-associated-types-constraints.md)
* Authors: [David Hart](https://github.com/hartbit), [Jacob Bandes-Storch](https://github.com/jtbandes), [Doug Gregor](https://github.com/DougGregor)
* Review Manager: TBD
* Status: **Awaiting review**

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

threads:

- [[Completing Generics] Arbitrary requirements in protocols](http://thread.gmane.org/gmane.comp.lang.swift.evolution/14243)
- [More Powerful Constraints for Associated Types](http://thread.gmane.org/gmane.comp.lang.swift.evolution/15201)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the Gmane web interface is back those links should be updated.

* [\[Completing Generics\] Arbitrary requirements in protocols](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/014667.html)
* [Proposal] More Powerful Constraints for Associated Types
  - [Week #1](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160418/015625.html)
  - [Week #2](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160425/015753.html)
  - [Week #3](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160502/016354.html)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do that asap!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

```

But as Douglas notes himself, that syntax is ambiguous since we adopted the
generic where clause at the end of declarations of the following proposal:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, distinguish the where word

Copy link
Collaborator Author

@hartbit hartbit Sep 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by that? Using where instead of where?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, 'cause it looks better :)

@@ -7,16 +7,24 @@

## Introduction

This proposal seeks to introduce a `where` clause to associated types declarations and improvements to protocol constraints to bring associated types the same expressive power as generic types.
This proposal seeks to introduce a `where` clause to associated types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop the “s” from “types”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

This proposal seeks to introduce a `where` clause to associated types declarations and improvements to protocol constraints to bring associated types the same expressive power as generic types.
This proposal seeks to introduce a `where` clause to associated types
declarations and improvements to protocol constraints to bring associated types
the same expressive power as generic types.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean "as generic type parameters"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -28,24 +36,36 @@ protocol Sequence {

## Detailed Design

First of all, this proposal modifies the grammar for protocols associated types to the following:
First of all, this proposal modifies the grammar for protocols associated types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"protocols'" or "a protocol's"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


```swift
protocol IntSequence : Sequence where Iterator.Element == Int {
protocol SomeSequence : Sequence where Iterator.Element == Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntSequence was a better name for this example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@DougGregor
Copy link
Member

Just in case anyone is curious what's going on: a review of the standard library found that the addition of this feature would be a significant clean-up for the standard library protocols. This clean-up would change some protocol requirements and the mangled names of most generics involving the Sequence protocol, so it has high ABI impact.

@jcnm
Copy link

jcnm commented Sep 19, 2016

It seem to me that use where keyword is too ambiguous. Even with the proposed new syntax.

Since we are building a new protocol with a settled constraint, a "with" instead of "where" keywords looks more precise. It allows us to bring back the simple equal sign "=" (used plfor assignation), which is semantically more accurate than the double equals "==" (used for comparison).

protocol IntSequence : Sequence 
with Iterator.Element = Internet {
//...
}

@DougGregor
Copy link
Member

@hartbit I have two smallish requests for you:

  1. Can you please add a section "Effect on ABI Stability" that mentions that there are a number of places in the standard library where this feature would be adopted (such as the SubSequence.Iterator.Element == Iterator.Element example), each of which will change the mangling of any generic function/type that makes use of the Sequence protocol?
  2. Then, can you post a proposal draft to swift-evolution for another short round of pre-review feedback? It's been quite a while since this has been discussed.

@hartbit
Copy link
Collaborator Author

hartbit commented Sep 20, 2016

It seem to me that use where keyword is too ambiguous. Even with the proposed new syntax.

@jcnm I really don't think it is worth adding a new keyword for that. But I suggest you bring it up during the mailing list discussions.

@DougGregor I'm on it.

@DougGregor DougGregor merged commit 1fb3715 into apple:master Sep 23, 2016
@hartbit hartbit deleted the associated-types-constraints branch January 28, 2017 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants