-
Notifications
You must be signed in to change notification settings - Fork 536
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
Conversation
*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 |
There was a problem hiding this comment.
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)"?
src/items/associated-items.md
Outdated
|
||
*Associated functions* are [functions] associated with a type. | ||
|
||
An *associated function declaration* is written as `fn`, then an [identifier] |
There was a problem hiding this comment.
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?
src/items/associated-items.md
Outdated
``` | ||
|
||
When the associated function is declared on a trait, the function can be called | ||
on the trait. When this happens, it is substituted for |
There was a problem hiding this comment.
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"?
src/items/associated-items.md
Outdated
let x: f64 = f64::from_i32(42); | ||
``` | ||
|
||
Associated functions whose first parameter is named `self` are called *methods* |
There was a problem hiding this comment.
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?
src/items/associated-items.md
Outdated
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: |
There was a problem hiding this comment.
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
src/items/associated-items.md
Outdated
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 |
There was a problem hiding this comment.
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.
src/items/associated-items.md
Outdated
|
||
An *associated constant definition* is written as a declaraction, but between | ||
the type and the `;`, there is an `=` followed by a [constant expression]. | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead link?
src/items/associated-items.md
Outdated
``` | ||
|
||
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/items/associated-items.md
Outdated
* `self` -> `self: Self` | ||
* `&self` -> `self: &Self` | ||
* `&mut self` -> `&mut Self` | ||
* `Box<self>` -> `self: Box<Self>` |
There was a problem hiding this comment.
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
src/items/associated-items.md
Outdated
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
bb3dec8
to
a8a40b8
Compare
Well, took me long enough, but I've addressed feedback where I can. |
That's a different semicolon. |
There was a problem hiding this 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.
src/items/associated-items.md
Outdated
|
||
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 `;`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function body?
src/items/associated-items.md
Outdated
|
||
* `self` -> `self: Self` | ||
* `&'lifetime self` -> `self: &'lifetime Self` | ||
* `&'lifetime mut self` -> `&'lifetime mut Self` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing a self
.
src/items/associated-items.md
Outdated
|
||
*Associated functions* are [functions] associated with a type. | ||
|
||
An *associated function declaration* declares a specification for an associated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'signature' instead of 'specification'
src/items/associated-items.md
Outdated
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 |
There was a problem hiding this comment.
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.
src/items/associated-items.md
Outdated
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. |
There was a problem hiding this comment.
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.
src/items/associated-items.md
Outdated
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"list of trait bounds"
src/items/associated-items.md
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a type
src/items/associated-items.md
Outdated
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. |
There was a problem hiding this comment.
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.
src/items/associated-items.md
Outdated
|
||
*Associated constants* are [constants] associated with a type. | ||
|
||
An *associated constant declaration* declares a specificatoin for associated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specificatoin -> specification
src/items/associated-items.md
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a type
Updated. |
Thanks! |
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.