An object by definition encapsulates mutable state. Even if a particular object happens to not contain any mutable state, there's no way to observe that from outside, by design.
If your language forces you to use an object to represent an (immutable) function value, all I can say is don't use such a shitty language.
If I read you correctly, your definition of "object" excludes each of the different concepts that C++, D, Python, and Abadí and Cardelli's object-calculus call "objects", but includes what Scheme, OCaml, and Kotlin call "functions"? You are of course welcome to use that definition, but it might improve communication to clarify that you're using the same word that other people use but with a completely different meaning.
> don't use such a shitty language.
There's a plausible reading of the GoF book and the whole "OO patterns" movement that it's largely about how to make do with shitty languages, though I'd nominate SICP as being better at that. Often languages that are shitty on one axis have compensating advantages on other axes—for example, although you can script Cocoa with cffi in Python, which is less shitty than Objective-C for scripting in general, you waste a lot more time debugging segfaults; despite the admitted aesthetic advantages of 68000 assembly with respect to amd64 assembly, the latter performs noticeably better on this laptop; and, while I think I've convincingly shown that a factory factory factory is dramatically more readable in OCaml than in Java, you're probably going to have a much easier time getting your remote dictionary server performance to scale to 16 cores in Java than in OCaml. If you have enough RAM, anyway.
That's not my intention; I'm trying to match the definition of an "object" in common programming languages. As far as I can see an object (instance) is essentially a bundle of method handles associated with an opaque (possibly empty) bundle of (possibly) mutable state. (In some languages the bundle might also include some visible state, but that doesn't change the essence of the thing, since that can be emulated with method handles - indeed Kotlin does exactly this). In Python's case the opacity of that state is a convention rather than a strict rule, but I think the general consensus would be that it should generally treated that way, and also that Python's objects are somewhat less object-ey than those of other languages. Certainly I'd say this definition is a close fit for C++ and Kotlin; what are you seeing as different there?
>>> class S(str): p = lambda me: len(me) + 3
...
>>> S("hi").p()
5
>>> S("uhoh").z = 3
>>>
You can't even detect this by enumerating the object's attributes with `dir()` or `.__dict__` at some moment — a method, or external code, can add new attributes at any later moment.(Hypothetically in CPython maybe you could disassemble the bytecode of a method to see if it tries to mutate the object or some global variable, but even if this is possible, you can also use cffi to change the value of 3 in CPython. I regard both of these as breaking the semantics of Python per se.)
So, I was wrong about Python. And, on further reflection, I was wrong about C++ as well, because even if you provide a `const`-argument override for a function, which is a thing you can do to observe whether an object is `const` or not from the perspective of your caller:
int x(int* foo) { return *foo + 3; }
int x(const int* foo) { return *foo + 2; }
// declaring as `const int i` changes program behavior:
int main(int argc, char **argv) { int i = 7; return x(&i); }
there's not even a compiler warning if your caller fails to do so as well: int x(int* foo) { return *foo + 3; }
int x(const int* foo) { return *foo + 2; }
// int y(int* foo) { return x(foo); }
int y(const int* foo) { return x(foo); }
int main(int argc, char **argv) { int i = 7; return y(&i); }
D's `immutable` is externally observable in the same way as `const` in D or in C++, but rigorously so in the sense we want here: invoking a function whose parameter is `immutable` with a mutable object is a compile-time error.Also, D's `immutable` is transitive, unlike C++'s `const`, but like, for example, E's DeepFrozen.
In the simple object calculus, everything is an object and there is no mutability at all. You could reasonably object (heh) that, although it's intended to capture object-oriented programming, and Abadí and Cardelli's book is called A Theory of Objects, it's not widely used, so opinions may differ as to whether they succeeded. https://news.ycombinator.com/item?id=26588642 goes into more detail there.
So your definition of "object" only excludes each of the different concepts that D and Abadí and Cardelli's object-calculus call "objects" (and I guess I should add E), not Python's or C++'s as well.
As for Kotlin, my claim about Kotlin was not about Kotlin "objects" but about Kotlin "functions", which are closures that can include access to mutable state, just as in Scheme and OCaml. I don't know Kotlin well enough to know whether your definition of "object" fits its definition of "object". It's an interesting question. Maybe it does! Kotlin's data classes, for example, can contain mutable attributes, and there really is no (non-reflective) way to observe them from another class — for example, for a method to refuse to accept an argument if it happens to be mutable in this way. Kotlin/Native evidently has a form of transitive and externally observable immutability, but I don't know much about it.
Your revision of the definition to include a bundle of method handles does seem to neatly exclude what are called "functions" in Scheme, OCaml, and Kotlin. I think it might also exclude objects in E, but since E is dynamically typed, that's sort of an implementation detail, and anyway E doesn't figure prominently in the consensus definition of the term "object".
Thank you for a very educational and stimulating discussion!
Although the GoF Abstract Factory pattern doesn't rule out having mutable state in your factory object, that is neither necessary for its purpose nor common practice when applying it in OO programming languages. The example in GoF is MotifWidgetFactory (for shitty Unix GUIs) vs. PMWidgetFactory (for shitty OS/2 GUIs), and in fact GoF says, "An application typically needs only one instance of a ConcreteFactory per product family," suggesting that they were thinking of cases where the factories don't even have immutable state such as the `terms` argument to `amb`.
They do explore a "prototype-based" factory that is stateful—but there the statefulness is just used as a way to configure the factory at startup, by building up a "product catalog" in it. Creating products doesn't modify the factory's state, and you could configure the "product catalog" just as easily by passing it in as an argument when the factory is instantiated; it's just that, in Smalltalk, that kind of thing is normally represented by segments of executable code that invoke a long sequence of side-effecting methods, rather than by data structures.
(You can also represent that kind of thing invoking a long sequence of pure functions, as in the "fluent interface" pattern that became popular with JQuery, but that isn't the Smalltalk way to do things, especially in 01994.)
The GoF examples other than the prototype factory are WidgetFactory and MazeFactory; in InterViews, WidgetKit, DialogKit, and LayoutKit; and in ET++, WindowSystem. Kerievsky's examples in Refactoring to Patterns (where he points out that people constantly conflate the various "factory" patterns, and that the lines between them are somewhat vague) are HTML parser AST node creation, ORM attribute descriptor creation, java.util.SynchronizedCollection and UnmodifiableCollection, and an OutputBuilder interface that allowed him to create either DOM nodes or XML output from the same unit test code. In all of these cases except the OutputBuilder, the factory objects are stateless.
So, by your definition that a factory is what you get if you take a function and add mutability, we find that 6 out of 7 of the GoF examples of "factories", and 3 out of 4 of Kerievsky's examples of "factories", are actually "functions" rather than "factories". I think this clearly shows that the categorization you propose does not coincide with the scriptural explanation of what "factory" means.
⁂
It's true that, in the case where there's no state, you can do this with just a pointer to a function. But pointers to functions don't exist at all in languages like Java, and aren't first-class values in languages like Pascal, where they are subject to various restrictions that keep you from using them like this.
Even in languages like C, which have first-class function pointers but not closures (except as a GCC extension subject to Pascal-like restrictions), you can't create new function pointers at run-time. So in C, or C++, you can replace a singleton factory like the ones GoF considers usual with a function pointer, or maybe a record of function pointers. But you can't write a factory factory that way, much less a factory factory factory.
So, while you're right that in a sense an immutable factory "is just" an indirectly-invoked function—that's the main point of my comment—if you want to do that in a language like Smalltalk or C++ (at least in 01994, when they wrote the book) or Java (at least in 02000), the Abstract Factory pattern in GoF tells you how to get it working.