4 ms·
lorry.pickup(&rustfest_envelope); lorry.done(); looks like methods on an object ("lorry") to me. Is it something else?
by Annatar 8y ago
lorry.pickup(&rustfest_envelope);
lorry.done();
looks like methods on an object ("lorry") to me. Is it something else?
- steveklabnik 8y agoYes. Rust does not have objects. Method call syntax isn’t inherent to objects.
- kjeetgill 8y agoInteresting. I'm coming from Java that looks-like an object to me. I assume method dispatch here is based on the "receiver" as well right? What differentiates it from an object? Not having identity? or Not being self describing (like a.getClass()) maybe? Thank you for the insight.
- steveklabnik 8y agoNo problem! Syntax is just syntax. Imagine a function that takes two 32-bit numbers, and adds them together: fn foo(x: i32, y: i32) -> i32 { x + y } You'd call it like this: foo(5, 6) // would give back 11 However, this is just syntax. There's no reason this can't be 5.foo(6) It's purely a different way of calling the same thing. The first argument goes before the dot. In some languages, this is 100% interchangable. In Rust, it's not 100%, but it's pretty true. For example, the wrapping_add method adds two numbers together, wrapping around if the number is too large. These are equivalent: let x: i32 = 5; assert_eq!(x.wrapping_add(5), 10); assert_eq!(i32::wrapping_add(x, 5), 10); You can use either method or function call style. These aren't objects, they're just primitive 32 bit numbers. There's no heap allocation, there's no extra bookeeping info. There are some more details, but that's the basic idea. Does that make sense?
- Annatar 8y agoWhat difference does it make how one calls it if I have to program the same way I'd have to program if I were using object oriented paradigm? Or even worse, if I'm forced to debug and understand code written in object-oriented style, how does that help debug and understand the program? It's a travesty this syntax or anything resembling object-oriented programming is allowed. By the by, the best programming languages aren't ones where there is more than one way to do it, but ones where there is one and exactly one way to do any given thing and the reason is simple: the code is easier to understand and therefore enhance or debug for anyone proficient in that language; in addition, it reduces the total number of mistakes in software written in such a language. AWK is one example of such a language.
- steveklabnik 8y agoBecause OO is more than method call syntax, so you wouldn’t be doing those things the same way that you would in an OO language, since Rust is not OO.