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 to allow self parameter be any type #1689

Closed
wants to merge 2 commits into from

Conversation

Projects
None yet
6 participants
@KalitaAlexey
Copy link

KalitaAlexey commented Jul 26, 2016

# Drawbacks
[drawbacks]: #drawbacks

No drawbacks.

This comment has been minimized.

@steveklabnik

steveklabnik Jul 26, 2016

Member

There is never a proposal with no drawbacks.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jul 26, 2016

I am not completely opposed to this idea, but this RFC is extremely light on details.


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.

@jonas-schievink

jonas-schievink Jul 26, 2016

Member

The RFC should define what "depends on Self" means

This comment has been minimized.

@KalitaAlexey

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.

@ticki

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.

@KalitaAlexey

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.

@ticki

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.

@KalitaAlexey

KalitaAlexey Jul 29, 2016

Author

@ticki I don't quite understood you. What is dot operator?

# Motivation
[motivation]: #motivation

It allows to avoid wrapper structs.

This comment has been minimized.

@jonas-schievink

jonas-schievink Jul 26, 2016

Member

An example showing where a wrapper struct is currently required would be nice

This comment has been minimized.

@KalitaAlexey

KalitaAlexey Jul 26, 2016

Author

@jonas-schievink See detailed design section.

@jonas-schievink

This comment has been minimized.

Copy link
Member

jonas-schievink commented Jul 26, 2016

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 Vec::len()? Will it not compile?

@KalitaAlexey

This comment has been minimized.

Copy link
Author

KalitaAlexey commented Jul 26, 2016

@steveklabnik I know but I can't figure out any more details.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Jul 26, 2016

The guarantee needs to be stronger, in order to be able to implement this in a meaningful way.
Last time I looked into this, Deref<Target=Self> seemed to be enough for inherent methods.
That means that the Self type is found when doing recursive dereferences of typeof self.

Trait methods, however, if they can be used in trait objects, require that typeof self is some X<Self> where X<Self>: CoerceUnsized<X<Trait>> where X<Self> and X<Trait> are structurally identical down to a matching pointer/reference to Self, and Trait, respectively.
Otherwise, associated types could be used to convert a pointer into a reference, in safe code.

cc @rust-lang/lang

@KalitaAlexey

This comment has been minimized.

Copy link
Author

KalitaAlexey commented Jul 26, 2016

@jonas-schievink It should be ambiguous code error.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Jul 26, 2016

@KalitaAlexey @jonas-schievink No, that method can't be in an impl MyStruct, at most impl [MyStruct]. It bears no direct relation to MyStruct, only to [MyStruct].

@KalitaAlexey

This comment has been minimized.

Copy link
Author

KalitaAlexey commented Jul 26, 2016

@eddyb As you wish it is an option also.

@nrc nrc added the T-lang label Jul 26, 2016

@nrc

This comment has been minimized.

Copy link
Member

nrc commented Jul 26, 2016

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.

@KalitaAlexey

This comment has been minimized.

Copy link
Author

KalitaAlexey commented Jul 27, 2016

As @nrc suggested I closed this PR and opened a thread on internals.rust-lang.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.