6 ms·
Objects in Functional Languages
- CyberDildonics 6y agoSince "object" is vague and ambiguous, this is talking about haskell's data types.
- GavinMcG 6y agoNo, it's not. It uses Haskell's data types but it's explicitly not about Haskell's data types. It also explicitly addresses two definitions of "object" so as to constrain vagueness.
- Geminidog 6y agoThis is describing issues and tradeoffs of OOP relative to FP and haskell. It conveniently side steps the fundamental issue with OOP as used in Java or C++ or C#. I agree with most of what the author says except that his OOP version is basically incredibly hard to read and not intuitive. However that's just my opinion. The real big problem with OOP in context of the popular definition is the promotion of using logical operations that are not combinators. This makes OOP less modular. class X: int b int c int d int x[100000] = {1,2,3,4,5,6,.......} function addOne(){ return b + c + 1 } The tradeoff with OOP is convenience. You have b and all the context of X implicitly accessible within the function but addOne cannot ever be used outside the context of X. To reuse addOne means to recreate the entire context of X. I want a banana but in order to get the banana I must recreate the gorilla holding the banana and the entire jungle the banana is sitting in. That's what most people complain about even when they don't realize it. In terms of the authors definition of OOP I don't think there's much controversy for that stuff.
- GavinMcG 6y agoDidn't the author address that? > In effect, the major selling point of the “object”-style design here is also a major downside: you don't have guarantees about the specific representation of data, which means your programs can easily mix-and-match different representations, but it also means that your program can't make use of representation-specific knowledge in ways that are advantageous. > There's another major concern as well, and that's that the specific choice of “object” representation can make a big difference in terms of what operations you can and cannot support.
- Geminidog 6y agoThe author is talking about representation of data, I'm talking about something different. I'm talking about scoping, grouping, modularity and free variables. y = 6 f(x) = x + y y is called a free variable and f is dependent on it. I cannot move f to another place because y will now be undefined. OOP is a more extreme version of this. class X y = 6 b = 8 f(x) = x + y Now f can't even be moved to another place without b. If you want to move f you have to move b as well. Combinators vs. free variables is the fundamental issue of OOP which this article writer effectively side steps with a unique definition of OOP where the method returns an entirely new object with differently defined methods. It's ok. It's worth talking about the trade offs of unique definitions of OOP but I think the author needs to mention that this is also not a mainstream definition and therefore sidesteps all the mainstream controversy.
- tcgv 6y agoI'm not sure I uderstand the point you are trying to make here: > To reuse addOne means to recreate the entire context of X. I want a banana but in order to get the banana I must recreate the gorilla holding the banana and the entire jungle the banana is sitting in. If 'banana' is dependent upon 'gorilla' then you're doing OOP wrong, or maybe you didn't need to reuse 'banana' in the first place and took some shortcuts while coding and now that you need you will have to refactor the code and extract that functionality into a new class. Even in the case of inaccessible logic hidden into classes it's entirely possible to extract that code into, say, an utility class that can be reused across several classes. In this case the resulting code may resemble a combination of higher level OOP classes wich take advantage of lower level functional modules for implementing their behavior.
- dnautics 6y agoYes, that's all true, but after even two stages of this you're in the kingdom of the nouns. Note the argument is not structural, but philosophical. Yes, the two constructs are logically isomorphic, but to get composability with objects you must start twisting your brain in uncomfortable ways. I would concede there might be a minority of folks whose brain truly works like this. For the rest, it must be stockholm syndrome.
- Geminidog 6y agoIt's structural. class X: constructor(y=3,z=2) y; z; b = [......] F(x) = y + x F cannot be reused outside of the context of X and z y and b. This is not a philosophical issue but concrete. G(x, y) = y + x Here G has the same exact logic as F but can be reused and is more modular. No philosophy of fuzzy stuff here I am talking about a concrete problem. Philosophical arguments lead to unresolved controversies. I am not going in that direction. I am talking about a concrete issue that can be resolved.
- dnautics 6y agoI think parents posters idea about reuse is that if you need to reuse it, you make it into an abstraction and hoist (copy/paste) to a superclass and make where you took it from a child class. On the one hand, it does make the reuse explicit ("explicit is better than implicit"). Or you broaden the member elements to be able to take a broader interface. On the other hand, this is how you wind up with enterprise java.
- blackrock 6y agoThe other thing about objects like this, is that the members b, c, d, and x, are actually global variables, within that object. Where any other method can overwrite its value at will, unless you force all modifications through another setter method, which will perform the error checking and validation. This is fine, when your object is just a toy object you learn in C++ 101, in school. But it becomes quite unwieldy, when working with actual real world business data. Unless you minimize the footprint of your object.
- gmfawcett 6y agoIt's odd to write about objects in functional languages with no mention of Ocaml. Perhaps the Scala and F# users will feel the same way... There's much research & experience re: objects in the FP space that isn't considered in this article.
- inopinatus 6y ago> You can use languages like Ruby ... in a way that matches this [Alan Kay] definition [of OOP], but they're not typically used this way Oh, but it is. Writing in a message-passing style is how I was taught Ruby, with specific reference to the ideas of Alan Kay and the influence of Smalltalk. This describes a tremendous amount of my code and of those I work with. It’s a very natural fit for Ruby and far from uncommon. Maybe that doesn’t apply to how folks are (initially) taught to write Rails apps, but that’s another can of worms.
- gglitch 6y agoTotally thought this was going to be about Erlang-style actors.
- bmitc 6y agoI am not really getting what the thesis of this article is, and rather than try to hone in on that, I figured I'd give some thoughts on what OOP in functional languages means to me. In terms of OOP, I primarily think of the paradigm being one that provides mechanisms for the encapsulation of both data and behavior and for relating these through inheritance or composition. The "only messaging" part of Kay's famous quote is too constraining in my view because it overly species the implementation details of the access of how one taps into an object's data and behavior. For most people, their OOP experience has seen them use reference-based OOP languages like C#, Java, and C++. In such cases, there is a distinct line between that and what's typically thought of functional programming. This jarring difference is probably best noticed in F# where you have the referenced-based OOP and arrays brought about by C# and .NET/CLR interoperability and the functional layer of immutable-by-default discriminated unions, tuples, and records in the rest of the language. However, my experience in OOP has centered around value-based OOP, and this is where the line between OOP and functional programming is blurred. This is because, in my view, one of the pillars of functional programming is that of immutable data. In a value-based OOP implementation, objects are merely immutable values, where methods are functions that take in the object and possibly some extra parameters and return a new one and possibly some extra data. It's just that classes and objects and the relationships that can be defined between them gives us the ability to encapsulate data and behavior and expose it in controlled ways. F# records are very nearly functional OOP when viewed this way. F# records can contain member functions, where are "methods" in that they act upon records through dot notation but they do not mutable the record passed in. F# records can also implement interfaces, which is inheritance that defines relationships between specifications of behavior. It's just that F# records expose and do not encapsulate the data and, by some extension, methods. I think value-based OOP has a place in functional languages. Records and tuples are product types that form new data by concatenating data, discriminated unions are sum types that form new data by or-ing data together through a flat hierarchy, and objects are types that form new data by collating data and behavior together with relationships formed between these types. If all one ever does to any of these is provide some functions, either defined externally as in functions operating on immutable types or internally in the case of member functions or methods, then this still satisfies one of the major tenets of functional programming, that being immutable and collections of functions operating on such data. I think Kay's thoughts on message passing lies at a higher level or is an adjacent idea. One sees such message passing in actor models such as Elixir and Erlang, but those processes do not have hierarchical relationships like objects do, although they do have compositional relationships, nor do they provide mechanisms for strictly and cleanly defining data the processes are responsible for like what classes do. In my experience, coupling a system like Elixir and Erlang's processes with value-based OOP is a huge win, because these things are really different but complementary ideas. In my opinion, OOP being about message passing isn't really the case. It just so happens that Smalltalk married these two ideas (OOP and message passing) together.
- jpcooper 6y agoI have failed to fully grasp this article. Is this definition of objects as defined by the set of functions which work on them any different from type classes in Haskell?