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

In libcore, convert functions to methods whereever it makes sense #6045

Closed
bstrie opened this issue Apr 24, 2013 · 10 comments
Closed

In libcore, convert functions to methods whereever it makes sense #6045

bstrie opened this issue Apr 24, 2013 · 10 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@bstrie
Copy link
Contributor

bstrie commented Apr 24, 2013

By "makes sense" I mostly mean "whereever the first argument to a function is the same type as the name of the module." So a function like str::to_chars("asdf") becomes "asdf".to_chars(), but vec::from_fn(5, |x| x) can be left as a function.

@bstrie
Copy link
Contributor Author

bstrie commented Apr 25, 2013

Some more information. Say there is a plus_one function in the int module:

fn plus_one(x: int) -> int {
    x + 1
}

You would first convert this to a method by pasting it into an impl and changing the name of the first argument:

impl int {
    fn plus_one(self) -> int {
        self + 1
    }
}

And then back in the original location redefine the function to do nothing except call the method:

fn plus_one(x: int) {
    x.plus_one()
}

Once all the functions are simply methods underneath, we can convert all the uses of functions in the compiler into methods, and then remove the functions entirely. But those latter two things are beyond the scope of this bug, just focus on creating the methods for now.

@emberian
Copy link
Member

I'm chugging away at this.

@emberian
Copy link
Member

This is currently blocked on #6068

@lucian1900
Copy link

This is nitpicking, but to me from_fn makes sense to be a static method on a struct, as an alternative constructor to new.

@emberian
Copy link
Member

Example, @lucian1900? I do not understand.

@emberian
Copy link
Member

Oh, vec::from_fn(5, |x| x)? What would it be instead? &[].from_fn(5, |x| x)? Static methods don't make sense to me. No reason not to have them namespaced, not in the struct.

@lucian1900
Copy link

It makes sense to me for types without a literal, like SomeVector::from_fn(5, |x| x)

@emberian
Copy link
Member

Sure, but not the builtins.

@emberian
Copy link
Member

emberian commented Aug 5, 2013

Visiting for triage; nothing to add

@bstrie
Copy link
Contributor Author

bstrie commented Aug 6, 2013

Closing this, as most of the egregious modules (str especially) seem to have been treated.

@bstrie bstrie closed this as completed Aug 6, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 24, 2020
…lip1995

Remove an extra blank line in `shadow_same`

It seems to be an extra blank line in doc example.

changelog: none

<img width="1143" alt="スクリーンショット 2020-09-15 8 50 30" src="https://user-images.githubusercontent.com/17407489/93149409-369df080-f731-11ea-9a39-d8bbc72b61ee.png">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants