The ontological problem of "does the knife cut the cucumber" or "does the cucumber get cut with the knife" seems serious. The "is-a" relationship is also problematic, because does a square inherit both from rhombus and parallelogram?
The ontological problem of "does the knife cut the cucumber" or "does the cucumber get cut with the knife" seems serious. The "is-a" relationship is also problematic, because does a square inherit both from rhombus and parallelogram?
There's your issue right there, "fashion". If we put aside fashion for a moment, we'll notice that all of the most heavily used languages are multi-paradigm. Trying to be purely-OOP or purely-functional, is intellectually and academically appealing, but not very pragmatic for a general-purpose language. In the end fashion comes and goes, but engineering remains.
Kinda both! The knife (aka: the "Cutter") can perform a "Cut" action on any object (aka: the "Cutable"), but the Cutable objects have to receive the Cut action and determine if the Cutter has enough "Cutter.Strength" to Cut the Cutable.
I think the failure of OOP is applying it to things that aren't appropriate or creating a Factory pattern that causes over-abstraction. OOP in itself isn't bad, much like PHP or Windows has their place even though they get shit on all the time, but the lack of care applying OOP (or any other pattern or architecture) is the bad part.
Rust's structs have methods and fields together, and that seems very much in fashion!
But, I wish that using OOP constructs to model ontologies would go out of fashion faster. The examples you pose are so abstract as to have no correct answer, because the correct answer depends on the needs of the software.
Cucumber/knife could be modeled in a thousand different ways depending on whether you're making The Sims, a recipe website, or the firmware for a robot chef.
"is-a" is indeed problematic, because "is" is a linguistic construct that can have different meanings, and there's never just a single "is-a" relationship. I'd go out on a limb and say you should never need classes for Square, Rectangle, etc; just make a Polygon that is a closed collection of points, and be done with it.
But that doesn't stop OOP education (from web tutorials to university courses) from teaching students to think about ontologies first and requirements second.
These are problems with the way OOP is often used, and with the language features that encourage this kind of usage. But I don't think it's necessary to say that classes in their entirety are an antipattern.
The problem with ontological purity is that it would need complicated features that aren't worth bothering with. Oval vs circle for example is a classic case of parameter dependent typing yet no language on this planet has objects whose type changes as it's data changes.
It is part of the job since it changes how (well) things get done.
> no language on this planet has objects whose type changes as it's data changes.
Yes there are. You might want to look into dependently typed languages.
I think that might be true, but I don't think that's usually the problem. I think the problem is that there is almost never one "true" ontology, there are usually multiple overlapping ontologies. And in most cases of real programs, you don't need an ontology anyway.
Most programs, if they need to handle ellipses as well as circles, should simply treat everything as an ellipse. If there is a specific time you need to know if something is a circle, write a function to check that. If performance is eaten up checking for circles repeatedly, sort the data into two lists of circular ellipses and non-circular.
IOW, there was never really a difference between
obj f(obj);
and class obj {obj f();}
so the whole idea was a mistake.Likewise,
obj.f()
should be synonymous with f(obj)
and the fact that in some languages it's not is evidence of a poorly thought-out abstraction. See multiple dispatch for the death blow.There's also a discussion elsewhere here about operator overloading, and how arithmetic operators don't behave like methods.
What?
Go has methods which "privilege" the first argument to functions:
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
Though it moves the argument out of the argument list and between the func keyword and method name.Rust also has a notion of methods:
https://doc.rust-lang.org/rust-by-example/fn/methods.html
Were you thinking of other languages?
It really is just syntax sugar,
Here’s another example. You’re writing a function `g` that accepts an object of type `Foo` and calls function `f` on it. You have a default implementation of `f`, but you also want to let the caller supply their own implementation. If you have some way of attaching behavior to a type — classes, typeclasses, etc — this is trivial. If not, your function has to explicitly ask for every single function from the caller.
And than you need functors and existential types to recreate classes… ;-)
Your reasoning is likely faulty if you don't see that OOP is a very advanced and great module system.
The only problem with OOP is that it's often misused for other things that aren't modules. But that's a different story.
And you can instantiate multiple instances of these "modules"?
struct foo;
That's an example of an opaque type. Put that in a header, and any function can make use of it in any other C file, they just can't access its innards (well, it's C so you actually can if you know how its contents are packed). Functions can accept them as parameters and return them knowing nothing but that opaque type and some additional function signatures like this (to do something useful with it): struct foo make_foo(...); // or struct foo*
void print_foo(struct foo*); // or struct foo
So I can write something like this: // main.c
#include "foo.h"
// other includes
void do_something(struct foo* f) {
// lots of code
print_foo(f);
}
int main(int argc, char* argv[]) {
struct foo f = make_foo(a_param);
do_something(&f);
return 0;
}
In a hypothetical C you could do something like this: struct foo {
int a;
char b;
private;
};
That last bit indicating that there is something there, but not what it is. Then somewhere you'd have a fuller definition: struct foo {
int a;
char b;
// private expanded
float c;
}
Which would be used by functions which are meant to actually operate on the rest of this structure. In this case, anything that knows of struct foo can directly access its "public" fields of a and b, but have no clue about the remaining fields.Maybe this would be seen as a violation of encapsulation, but to make the two equivalent requires the ability to allow private variable access at the call cite.
In very simple words, each object in an OOP program is a virtual computer, its class define the instruction set.
"Objects are a recursion on the notion of computer itself"
--Alan Kay
OOP languages never actually include this because nobody seems to particularly care if their code actually works.
Classes are also nicer to deal with in dynamically-typed languages, or at least ones with very smart automatic static typing. You don't get stuck dealing with all that pedantry.
Btw, your example works fine in OOP. It'd be parallelogram -> rhombus -> square. But yes there are examples of it not working well.