Skip to content

Move associated items to their own page. #214

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

Merged
merged 3 commits into from
Feb 12, 2018

Conversation

Havvy
Copy link
Contributor

@Havvy Havvy commented Jan 23, 2018

Because they are a cutting concern between impls and traits, they don't really fit in either.

If anybody has concrete suggestions for how to make this better, I'm all ears. I'm not a fan of a lot of my writing in this PR. Especially the definitions.

*Associated Items* are the items declared in [traits] or defined in
[implementations]. They are called this because they are defined on an associate
type — the type in the implementation. They are a subset of the kinds of
items you can declare in a module. Specifically, there are [associated
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps "associated functions (including methods)"?


*Associated functions* are [functions] associated with a type.

An *associated function declaration* is written as `fn`, then an [identifier]
Copy link
Contributor

Choose a reason for hiding this comment

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

This applies throughout the doc: spelling grammar out explicitly in words seems verbose and hard to read. Moreover, this seems redundant with regular function grammar. Do we have a BNF standard we can use? Or perhaps update the grammar page and link to that?

```

When the associated function is declared on a trait, the function can be called
on the trait. When this happens, it is substituted for
Copy link
Contributor

Choose a reason for hiding this comment

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

"on both the trait and on any type implementing the trait"?

let x: f64 = f64::from_i32(42);
```

Associated functions whose first parameter is named `self` are called *methods*
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like methods should be their own shorthand, perhaps?

and may be invoked using the [method call operator], for example, `x.foo()`, as
well as the usual function call notation.

When the first parameter is named `self`, the following shorthands may be used:
Copy link
Contributor

Choose a reason for hiding this comment

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

Are lifetimes permitted in shorthands? e.g. &'a self

An *associated type definition* is written as `type`, then an [identifier], then
an `=`, and finally a [type].

If an item `Item` has an associated type `Assoc`, then `Item::Assoc` is a type
Copy link
Contributor

Choose a reason for hiding this comment

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

Does Self count as a built-in associated type? I feel like it should be mentioned here.


An *associated constant definition* is written as a declaraction, but between
the type and the `;`, there is an `=` followed by a [constant expression].

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps add more discussion of how to refer to associated constants, as you did with types? e.g. Item::Const

when called evaluates to a direct call to the function.

[function item type]: types.html#function-item-types
[*function item type*]: types.html#function-item-types
Copy link
Contributor

Choose a reason for hiding this comment

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

Move the formatting outside the link makes more sense to me, perhaps. I wasn't even aware this link syntax worked with formatting.

@@ -0,0 +1,260 @@
# Associated Items

*Associated Items* are the items declared in [traits] or defined in
Copy link
Contributor

Choose a reason for hiding this comment

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

Dead link?

```

This defines a trait with two methods. All values that have [implementations]
of this trait in scope can have their `draw` and `bounding_box` methods called.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is an implementation being in scope defined anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You misread; it's the traits that have scope (visibility?), not the implementations. Either way, gonna have to reword this to avoid that confusion.

* `self` -> `self: Self`
* `&self` -> `self: &Self`
* `&mut self` -> `&mut Self`
* `Box<self>` -> `self: Box<Self>`
Copy link
Contributor

Choose a reason for hiding this comment

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

Box<self> isn't valid

An *associated type definition* is written as `type`, then an [identifier], then
an `=`, and finally a [type].

If an item `Item` has an associated type `Assoc`, then `Item::Assoc` is a type
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately this doesn't work usually (except for type parameters, which are special). The <Type as Trait>::Item syntax needs to be used.

Copy link
Contributor

@matthewjasper matthewjasper left a comment

Choose a reason for hiding this comment

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

A few technical things. Might give some comments on the writing later.

@Havvy Havvy force-pushed the assoc-items branch 2 times, most recently from bb3dec8 to a8a40b8 Compare February 2, 2018 01:42
@Havvy
Copy link
Contributor Author

Havvy commented Feb 2, 2018

Well, took me long enough, but I've addressed feedback where I can.

@Havvy
Copy link
Contributor Author

Havvy commented Feb 2, 2018

The TravisCI failure is not against the latest change. Seems I might have confused TravisCI with multiple pushes to my branch. >_>

That's a different semicolon.

Copy link
Contributor

@matthewjasper matthewjasper left a comment

Choose a reason for hiding this comment

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

Thoughts reading through this again. Mostly wording suggestions.


An *associated function declaration* declares a specification for an associated
function definition. It is written as a function item, except the
function block is replaced with a `;`.
Copy link
Contributor

Choose a reason for hiding this comment

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

function body?


* `self` -> `self: Self`
* `&'lifetime self` -> `self: &'lifetime Self`
* `&'lifetime mut self` -> `&'lifetime mut Self`
Copy link
Contributor

Choose a reason for hiding this comment

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

missing a self.


*Associated functions* are [functions] associated with a type.

An *associated function declaration* declares a specification for an associated
Copy link
Contributor

Choose a reason for hiding this comment

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

'signature' instead of 'specification'

function definition. It is written as a function item, except the
function block is replaced with a `;`.

The identifier if the name of the function. The generics declares types for
Copy link
Contributor

Choose a reason for hiding this comment

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

This paragraph is referring to removed text.

The identifier if the name of the function. The generics declares types for
usage in the rest of the function declaration. The generics, parameter list,
return type, and where clause must be the same in the associated function
definition.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is some flexibility here from subtyping and weaker/equivalent where clauses, but this is probably OK for now.


An *associated type declaration* declares a specification for associated type
definitions. It is written as `type`, then an [identifier], and
finally an optional trait bounds.
Copy link
Contributor

Choose a reason for hiding this comment

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

"list of trait bounds"

An *associated type definition* defines a type alias on another type. It is
written as `type`, then an [identifier], then an `=`, and finally a [type].

If an item `Item` has an associated type `Assoc` from a trait `Trait`, then
Copy link
Contributor

Choose a reason for hiding this comment

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

If a type

If an item `Item` has an associated type `Assoc` from a trait `Trait`, then
`<Item as Trait>::Assoc` is a type that is an alias of the type specified in the
associated type definition. Furthermore, `Item::Assoc` can be used in type
parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

If Item is a type parameter.


*Associated constants* are [constants] associated with a type.

An *associated constant declaration* declares a specificatoin for associated
Copy link
Contributor

Choose a reason for hiding this comment

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

specificatoin -> specification

The identifier is the name of the constant used in the path. The type is the
type that the definition has to implement.

An *associated constant definition* defines a constant associated with another
Copy link
Contributor

Choose a reason for hiding this comment

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

a type

@Havvy
Copy link
Contributor Author

Havvy commented Feb 12, 2018

Updated.

@matthewjasper matthewjasper merged commit e2e3652 into rust-lang:master Feb 12, 2018
@matthewjasper
Copy link
Contributor

Thanks!

@Havvy Havvy deleted the assoc-items branch February 19, 2018 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants