3 ms·
> Rust "feels" OO but isn't. If you write C#7 or Swift and avoid inheritance and prefer immutable types as long as possible - how OO is your code then? Both ar
by jcelerier 9y ago
> Rust "feels" OO but isn't. If you write C#7 or Swift and avoid inheritance and prefer immutable types as long as possible - how OO is your code then?
Both are 100% object-orientated (and in the case of Rust, also 100% FP is reachable I'd say. Not knowledgeable enough in C# to say). Do they have in-memory data structures with attached functions that do useful work with this object's state (not necessarily mutating it) ? Yes. Then OO.
- nonsince 9y agoRust is neither OO nor FP, it's procedural (like C and Fortran) but borrows concepts from OO and FP. OO requires data structures to support dynamic dispatch, and although you can have dynamic dispatch in Rust via trait objects your code is heavily gimped - no generics, you can only pass by reference, lifetimes have to be explicitly annotated, etc. It feels like OOP-emulating C code from back in the day. Likewise, if you try to do pure functional programming in Rust you're going to have a bad time. Closures don't share types, and doing dynamic dispatch on them creates the same problems as trying to emulate OOP. In reality it's its own beast, much closer to C with templates than a crossbreed of C++ and Haskell.
- pjmlp 9y agoContrary to mainstream knowledge there isn't one single truth of what OOP or FP actually are, rather multiple approaches of applying a set of abstract concepts.
- boramalper 9y agoThis, a million times. Any (meta) discussion on OOP without giving a precise definition of it is futile, for the devil lies precisely in those details that are omitted from the discussion.
- dom0 9y ago> it's procedural (like C and Fortran) Wait... do you actually believe that? Tons of C software is OO through and through, has been since C existed.
- zaphar 9y agoI'm not sure where this meme on HN that Rust isn't OO comes from but it's based on a definition of OO that is almost never applied by day to day programmers and it's disagreed with by a fair number of language theory people. Rust has encapsulation, message passing in the smalltalk/OO sense, and polymorphism. It does this through objects with attached methods, and traits. In every way that actually matters to a working programmer Rust is OO.
- ettoretorti 9y agoThere's more to the everyday definition object oriented than having functions operating on in-memory data structures. Two other features that are commonly included are subtyping through inheritance and encapsulation. If we stick to your definition C functions operating on structs qualify as OO, and I don't think many would agree with that.
- seanmcdirmid 9y agoMy first exposure to OOP was in C using structures to hold function pointers as a pattern. You can do OO in C without C being OO. There is a huge semantic difference between doing OO and a language at supports OO, the article is referring to the former rather than the latter.
- tome 9y ago> Do they have in-memory data structures with attached functions What do you mean by "attached function"?
- jcelerier 9y agoI meant that a semantic value in the code (a variable for instance) "carries" functions with it, as methods. e.g. in a C API: struct foo { // dispatch void (*do_stuff)(int); int (*get_stuff)(const char*, void* context); }; // attached functions void foo_do_operation1(foo*); int foo_do_operation2(foo*, int); or C++ which allow to conflate both forms but restricts the first with inheritance struct foo { virtual void do_stuff() = 0; virtual int get_stuff(std::string, a_better_type_than_void_ptr&) = 0; void do_operation1(); int do_operation2(int); }; or JS where everything can change whether you want it or not var v = new Object; v.do_stuff = function() { ... } v.get_stuff = function(str, something) { ... } v.do_operation1 = function() { /* v is available in the closure */ } etc... or OCaml modules: module foo = struct let do_operation1 = ... let do_operation2 x = ... end in every case, some state is mentioned first, and then some code is bound to this state; then, the language (except in C where it is only a convention... even though some people can get fairly "creative" with macros) provides a way to call the code with a given variable of type `foo`.