4 ms·
> Yup, really. Classes are just higher order functions, which accept arguments (constructor) and return a list of functions that can manipulate those arguments
by kazinator 6y ago
> Yup, really. Classes are just higher order functions, which accept arguments (constructor) and return a list of functions that can manipulate those arguments (methods).
Nope, really they are not. Rather, objects are run-time, publicly accessible environments (bindings of names to slots).
Since functions also have environments, we can come up with some contrived use cases whereby a rigorously encapsulated OOP example revolving around a class that is sealed against extension and uses no inheritance or other OOP mechanisms is nicely mimiced by a vector of functions that share the same environment.
But imitating some facets of a thing is not being that thing. A dog standing on hind legs isn't a man.
A more fully featured OOP can be built up, which yet keeps using lexical variables for slots. Public access to slots by name can be implemented by having a standard function supported at the same position in the function table, which takes a symbol argument, and switches on it to map it to a variable in its environment.
Providing possible implementation mechanisms for thing is also not being that thing.
We can easily reverse this, too, and claim that functions are just objects. Compilers for lexical closures do things like flatten the environment into an array in which items are accessed by position. So, first class functions are, under the hood, nothing but code, plus an array, in machine language. And since data structures are also some code offsetting into arrays, it's all the same.
You know, the moment I saw a NAND gate schematic, my mind opened to an amazing revelation: we don't need both zeros and ones, just zeros and NAND gates! The number one is nothing but a zero, duplicated into the two inputs of a NAND gate.
- ookdatnog 6y ago> You know, the moment I saw a NAND gate schematic, my mind opened to an amazing revelation: we don't need both zeros and ones, just zeros and NAND gates! The number one is nothing but a zero, duplicated into the two inputs of a NAND gate. This is not as absurd as you think it is. The most commonly used definition of the natural numbers (https://en.wikipedia.org/wiki/Peano_axioms#Formulation https://en.wikipedia.org/wiki/Peano_axioms#Formulation) is structured in a way similar to your description: we have a single explicitly declared natural number (zero), and all other natural numbers are defined as a sequence of function applications on zero (ie 4 is defined as S(S(S(S(zero))))). Our notation for numbers is just "syntactic sugar" on top of this representation: useful when doing practical work with numbers, but only a hindrance when you're studying the concept of "number" itself. Analogously, in programming language theory (studying the properties of programming languages moreso than trying to do program things in them), we often prove properties of languages by studying extremely frugal, tiny languages (often called "calculi"), that only have the absolute minimum primitive constructs. Realizing that a construct can be expressed as "syntactic sugar" on top of a simpler one (objects on top of closures, for instance), is useful in this setting because it reduces the number of cases in proofs.
- stopachka 6y agoFantastic explanation ookdatnog, thank you!
- fhjfyjfgnh 6y agoExcept that you can't define an NAND gate without defining an output voltage for each of 0 and 1. You can't bootstrap +5V into existence ...and for that matter you can't define a NAND gate without specifying what input voltage will be interpreted as a 1. It is quite as ridiculous as kazinator intended. But great mansplanation, carry on.
- LudwigNagasena 6y agoBut it doesn’t matter what you use to bootstrap something because we talk about abstractions. And it is absolutely true that NAND an F form a functionally complete set of gates. > But great mansplanation, carry on. What a weird way to be overtly sexist when you don’t even know the genders of the people involved.
- ookdatnog 6y agoYou can define logic circuits abstractly, without referring to their implementation in electronics at all. Even if you insist on defining them in terms of electronics, I don't think there's a significant difference between assigning some meaning to an arbitrary voltage vs assigning some meaning to an arbitrary squiggle on a piece of paper.
- ithinkso 6y agoThis touches on an interesting topic I've been thinking about for quite some time now. I have a somewhat reverse feeling about the definition of natural numbers (or for that matter, about the 'foundation of mathematics' as in ZFC set theory or whatever you want to choose) than you've presented above. Those definitions were developed after we had something useful in order to 'codify' it and make it more precise. They are not, in my opinion, a basic blocks that the rest is built from but an added layer of abstraction on top of whathever it is you're defining. I don't mean I don't understand set theory and the role it has, or advocating for any other system, I absolutely love it. What I mean is, in practical things (programming languages etc.), it feels a bit counter-productive to me to reduce them to the simplest possible definitions/terms (that have to fit to the particular use case by definition), declaring that a building block and then explaining every other concept in those terms as if it was a proof that we have trully found a foundation and the rest is just a 'syntactic sugar' edit: You all probably know this famous gif of a ballerina that can rotate to the left or to the right depending on how you think about it. After writing the above post the notion of S(S(S(S(0)))) keeps flipping in my brain between being an abstraction of and simplification of natural numbers. It's weird.
- nine_k 6y agoOne way to think about object instances is that they are namespaces full of partially applied functions, the applied argument being `this` or `self`; Python makes it excessively obvious. The function that produces these namespaces is usually the `new` operator, its argument, the "constructor", just allows to set up the common partially applied value (and run arbitrary code along the way). This partial application is not a serious problem; the mutability and the inheritance are.