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

RFC: Bounds on trait objects should be separated with + #87

Merged
merged 2 commits into from
Jun 11, 2014

Conversation

pcwalton
Copy link
Contributor

  • Start Date: 2014-05-22
  • RFC PR #: (leave this empty)
  • Rust Issue #: (leave this empty)

Summary

Bounds on trait objects should be separated with +.

Motivation

With DST there is an ambiguity between the following two forms:

trait X {
    fn f(foo: b);
}

and

trait X {
    fn f(Trait: Share);
}

See Rust issue #12778 for details.

Also, since kinds are now just built-in traits, it makes sense to treat a bounded trait object as just a combination of traits. This could be extended in the future to allow objects consisting of arbitrary trait combinations.

Detailed design

Instead of : in trait bounds for first-class traits (e.g. &Trait:Share + Send), we use + (e.g. &Trait + Share + Send).

Drawbacks

It may be that + is ugly. Also, it messes with the precedence of as. (See Rust PR #14365 for the fallout of the latter.)

Alternatives

The impact of not doing this is that the inconsistencies and ambiguities above remain.

Unresolved questions

Where does the 'static bound fit into all this?


# Detailed design

Instead of `:` in trait bounds for first-class traits (e.g. `&Trait:Share + Send`), we use `+` (e.g. `&Trait + Share + Send`).
Copy link
Member

Choose a reason for hiding this comment

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

+ would be commutative, right? so &Share + Send + Trait would also work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, eventually. In the first implementation it will not.

@alexcrichton
Copy link
Member

An alternative which may want to be mentioned is to require that arguments have names or require that they don't (depending on the context). I don't believe this is the right way to go, however.


# Drawbacks

It may be that `+` is ugly. Also, it messes with the precedence of `as`. (See Rust PR #14365 for the fallout of the latter.)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we define as as taking a subset of types that simply excludes Foo + Bar? You can then use x as (Foo + Bar) if you need that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we could bifurcate the grammar of as; perhaps it could just take a
PATH or a parenthesized type.

@chris-morgan
Copy link
Member

I had hoped to have T + T syntax for anonymous sum types. I guess | should work just as well there… if I ever get to writing a formal RFC, anyway…

@sfackler
Copy link
Member

I personally prefer @alexcrichton's alternative of requiring names.

@lilyball
Copy link
Contributor

@sfackler Requiring names on function arguments seems potentially ok, but what about a function argument that's a closure? Requiring names on the arguments to that closure seems extreme, seeing as how every single closure function argument I've seen has skipped the names.

@sfackler
Copy link
Member

Yeah, I guess that wouldn't fix this issue.

@pcwalton
Copy link
Contributor Author

pcwalton commented Jun 9, 2014

Updated this PR to add a type grammar restriction.

@nikomatsakis
Copy link
Contributor

@pcwalton the more I think about it, the more I like the idea of the grammar saying something like this:

Ident = ID
IdentList = ID | '(' ID { '+' ID } [ '+' ')'

in other words, requiring parentheses around +. It just seems to always read better to me:

&(Write+Send)
|int|:(Send+Share) -> int

etc

@nikomatsakis
Copy link
Contributor

Well, except for Box<Trait+Send>. That should still work. :) I guess I just mean having a +-free grammar underneath & and other unary operator sigils.

@alexcrichton
Copy link
Member

Accepted per our discussion in yesterday's meeting

withoutboats pushed a commit to withoutboats/rfcs that referenced this pull request Jan 15, 2017
Don't store an Arc in ReadinessStream
@Centril Centril added A-syntax Syntax related proposals & ideas A-traits Trait system related proposals & ideas A-trait-object Proposals relating to trait objects. labels Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntax Syntax related proposals & ideas A-trait-object Proposals relating to trait objects. A-traits Trait system related proposals & ideas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants