4 ms·
The more I used C, the more I ended up using structs as instances and functions that looked like: void mything_somefunction(MyThingStruct *mything, ...args
by nomel 6y ago
The more I used C, the more I ended up using structs as instances and functions that looked like:
void mything_somefunction(MyThingStruct *mything, ...args){}
The weirdest thing was some of the crusty C guys in my group saying "Eww, is that objects?" No, it's maintainable, and it avoids collisions.
- dnautics 6y agoThat's really strange. In my era I knew a lot of C programmers who specifically advocated this as a "better way to do objects" than C++. Even some who in a pinch implemented their own v-tables, freely admitting that occasionally copping an idea from C++ and turning into an explicit contract is a thing (though this was painful enough that you would rightfully do this rarely). It's even more strange now, that we have distinctly non-OO languages that explicitly use the first-parameter-is-struct pattern, e.g: struct definition: https://github.com/elixir-lang/elixir/blob/v1.10.4/lib/elixir/lib/calendar/date.ex#L54 https://github.com/elixir-lang/elixir/blob/v1.10.4/lib/elixi... and functions: https://hexdocs.pm/elixir/Date.html#functions https://hexdocs.pm/elixir/Date.html#functions
- vbezhenar 6y agoAt this point I don't understand why people don't just use C++ for such a code. Of course bringing all C++ features is another matter. But getting some object-oriented C code and transforming it into C++ code is pure win.
- thu2111 6y agoA lot of it is/was driven by the historical instability of the C++ ABI, itself driven by instability in the language. If you made an object with a C based pattern, you could export a binary compatible interface and use dynamic linking. If you exported the same object as C++, even if it boiled down to the same thing, your users might get crashes because two compilers disagreed on the right way to compile it. That's the origin of COM. It's basically the C mything_mystruct pattern with a lot of additional stuff on top. Actual COM objects were usually implemented in C++ though.
- pkaye 6y agoI'm okay with this type of stuff but when people start to stretch it to support inheritance and virtual functions and then clever macros to hide it all to make it look like C++ its too much.
- imtringued 6y agoSomething a lot of people seem to forget is that classes are basically the combination of data and functions into a single type dependent namespace. In OOP you can have multiple functions that are called "save" and they are primarily distinguished by their type. If you define a static function in Java you call it by invoking its namespace: MyThing.somefunction(myThing, args) Alternatively your function is bound to an instance of an object. Then the save function is chosen based on the type of the variable you are calling the function on. Compare this to Haskell records. Field names are not defined inside a type dependent namespace and therefore you cannot reuse field names in a second record. If I remember type classes correctly they allow you to define implementations sharing the same function name, but they don't allow you to have multiple type classes define a function with the same name so they do not offer type dependent namespaces either. OOP tends to make organization easier because of this namespacing feature. It probably has led to "over-organized" codebases though. Too much of a good thing can also backfire.
- tome 6y ago> you cannot reuse field names in a second record Sure you can, it just has to be defined in a separate module (because Haskell namespaces correspond directly to modules). It's still annoying that types don't introduce a separate namespace but it is merely annoying.
- pjmlp 6y agoThat is basically ADT based programming as made popular by languages like Modula-2 and USCD Pascal, as Abstract Data Types, where mything is a module. That is the thing, at their basic level, objects are just some form of extensible modules.