Re:Can you use a class in C?
by commonlisp94 on Sunday August 13, 2023 @10:48AM
(#37110528)
It would make a huge difference if that style were used more in practice.
OOP is just functions that are only polymorphic in 1 argument, when you want it on all of them.
Multiple dispatch for the win...
Also see:
Effective C++ (by Scott Meyers) Item 23:
“Prefer non-member non-friend functions to member functions. Doing so increases encapsulation, packaging flexibility, and functional extensibility.”
The text has a lot more detail but that’s a brief summary. Folks just need to read the literature then this sort of knowledge would be in common use.
One downside of language evolution is a lot of people focusing on new language features, etc, but then some of this older, important knowledge gets skipped over.
Re:Can you use a class in C?
by iraqmtpizza on Sunday August 13, 2023 @07:42PM
(#37115763)
is it not apparent that a.distance(b) and b.distance(a) should both exist? if they don't, the API is poorly designed
If you want to do generic programming, it's much better to write f(a, b) instead of a.f(b).
Suppose I'm writing a generic algorithm that needs some function distance(A a, B b) for generic A and B. Distance is probably a commutative function, why would a.distance(b) be better than b.distance(a)? It's arbitrary.
Secondly, if functions are first-class citizens, and I don't "own" the types A and B, it's much easier to implement distance(A a, B b) myself than it is to extend the types A and B with a member function `distance`.