Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC to allow self parameter be any type #1689
Conversation
and others
added some commits
Jul 26, 2016
steveklabnik
reviewed
Jul 26, 2016
| # Drawbacks | ||
| [drawbacks]: #drawbacks | ||
|
|
||
| No drawbacks. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I am not completely opposed to this idea, but this RFC is extremely light on details. |
jonas-schievink
reviewed
Jul 26, 2016
|
|
||
| Currently self parameter type is allowed to be only `Self`, `&Self`, `&mut Self`, `Box<Self>`. | ||
|
|
||
| Solution: Allow `self` be any type that depends on `Self`. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
KalitaAlexey
Jul 26, 2016
•
Author
@jonas-schievink Type depends on Self = There is Self in Type. For example:
Vec<Self>, Box<Self>, MyStruct<Self>.
This comment has been minimized.
This comment has been minimized.
ticki
Jul 27, 2016
Contributor
But, how would you construct a value of Vec<Self> from a value of Self? Clearly, that doesn't make sense.
What you would need is a trait, like From<Self> which defines that self is constructed from the value in question, but at that point you introduce hidden conversions.
I believe that some form of generalized AsRef is what's needed.
This comment has been minimized.
This comment has been minimized.
KalitaAlexey
Jul 27, 2016
Author
@ticki I can't understand you.
Type depends on Self = Type has Self as generic parameter or associated type.
This comment has been minimized.
This comment has been minimized.
ticki
Jul 29, 2016
Contributor
Yes, but you need to define how to construct the value of that type when using the dot operator.
This comment has been minimized.
This comment has been minimized.
jonas-schievink
reviewed
Jul 26, 2016
| # Motivation | ||
| [motivation]: #motivation | ||
|
|
||
| It allows to avoid wrapper structs. |
This comment has been minimized.
This comment has been minimized.
jonas-schievink
Jul 26, 2016
Member
An example showing where a wrapper struct is currently required would be nice
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
What happens when I do this: struct MyStruct;
impl MyStruct {
pub fn len(self: &Vec<MyStruct>) -> usize { ... }
}
fn main() {
let v: Vec<MyStruct> = Vec::new();
v.len(); // which method is called?
}Will this shadow |
This comment has been minimized.
This comment has been minimized.
|
@steveklabnik I know but I can't figure out any more details. |
This comment has been minimized.
This comment has been minimized.
|
The guarantee needs to be stronger, in order to be able to implement this in a meaningful way. Trait methods, however, if they can be used in trait objects, require that |
This comment has been minimized.
This comment has been minimized.
|
@jonas-schievink It should be ambiguous code error. |
This comment has been minimized.
This comment has been minimized.
|
@KalitaAlexey @jonas-schievink No, that method can't be in an |
This comment has been minimized.
This comment has been minimized.
|
@eddyb As you wish it is an option also. |
nrc
added
the
T-lang
label
Jul 26, 2016
This comment has been minimized.
This comment has been minimized.
|
I believe this has previously been discussed in the context of UFCS, it would be good to dig up and reference that discussion. This RFC is more at the 'feature request' stage rather than ready for an RFC PR. I would recommend the author close it for now and open either an issue on this repo or a thread on internals.rust-lang.org in order to gather the details required for an RFC. |
This comment has been minimized.
This comment has been minimized.
|
As @nrc suggested I closed this PR and opened a thread on internals.rust-lang.org. |
KalitaAlexey commentedJul 26, 2016
•
edited
Rendered