8
you are viewing a single comment's thread
view the rest of the comments
[-] armchair_progamer@programming.dev 7 points 6 months ago* (last edited 6 months ago)

I could understand method = associated function whose first parameter is named self, so it can be called like self.foo(…). This would mean functions like Vec::new aren’t methods. But the author’s requirement also excludes functions that take generic arguments like Extend::extend.

However, even the above definition gives old terminology new meaning. In traditionally OOP languages, all functions in a class are considered methods, those only callable from an instance are “instance methods”, while the others are “static methods”. So translating OOP terminology into Rust, all associated functions are still considered methods, and those with/without method call syntax are instance/static methods.

Unfortunately I think that some people misuse “method” to only refer to “instance method”, even in the OOP languages, so to be 100% unambiguous the terms have to be:

  • Associated function: function in an impl block.
  • Static method: associated function whose first argument isn’t self (even if it takes Self under a different name, like Box::leak).
  • Instance method: associated function whose first argument is self, so it can be called like self.foo(…).
  • Object-safe method: a method callable from a trait object.
[-] PlexSheep@infosec.pub 3 points 6 months ago* (last edited 6 months ago)

I'd call instance methods just methods, it fits more into how the word is commonly used.

The omission is just way to easy not to use.

[-] kevincox@lemmy.ml 1 points 6 months ago

Yes, I agree with your clarification of commonly in use terms.

this post was submitted on 11 May 2024
8 points (78.6% liked)

Rust

6029 readers
3 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS