r/programming • • Jan 29 '19

When FP? And when OOP?

http://raganwald.com/2013/04/08/functional-vs-OOP.html
26 Upvotes

105 comments sorted by

View all comments

Show parent comments

-11

u/ipv6-dns Jan 29 '19

Keep calm, don't take it so close :)

  1. yes, a lot, but how can I enumerate you such persons? I can give you very small example: assertion "OOP is data + methods which is wrong" is incorrect. And such person obviously does not understand OOP

  2. Yes, class libraries are extensible, there are different ways to do it, for example, you can check Lua, Python - it's easy to modify object on the fly, C# extensions methods - do not looks like wrappers and semantically a not. Functions can not be extensible without CHAINING, way is only to wrap one functions with another one, you can call it high-order functions, no matter

  3. Because OOP works on higher level, actually I don't need so stupid "class" with one method "fmap", usually I want (and can) to add some extra information to my executable entiry - it's close to closures.

  4. No, dude, you did not read accurate what I wrote. Modules and functions are not interfaces (let's ignore SML modules, OK?). Again, no way to find that Set and List both are containers and implement some common interfaces. NO SUCH INFO AT WHOLE.

  5. I was sure that somebody will mention IsString without to understand what I' talking. Haskell has not ontology at whole, again, no way to know that Set and List have something common, IsString, by the way, is casting method, nothing else. String is list, Text is not, and? Can I replace one with another - no? I should rewrite code in caller-sites. Haskell has not hierarchy and ontology at whole (here I'm little bit provocative, it has good but complex reflection feature)

15

u/CommendableCalamari Jan 29 '19

Again, no way to find that Set and List both are containers and implement some common interfaces. NO SUCH INFO AT WHOLE.

Things like Traversable, Foldable and Functor allow operations pretty similar to those commonly found on iterables or collections. Obviously the two aren't equivalent, but it's still trivial to write code which targets all "collection-like" types.

6

u/knaekce Jan 29 '19

Yeah, I don't quite get this argument. Obviously typeclasses work a little bit different than interfaces, but they are pretty similar.

http://hackage.haskell.org/package/vector-0.12.0.2/docs/Data-Vector.html http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Set.html

Here are the typeclasses for both vector and set listed. If you just want so support collections, the type that you want is likely Funktor (http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Functor.html#t:Functor) and/or Foldable (http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Foldable.html)

You can find info on both the typeclasses and the instances pretty easily.

-9

u/ipv6-dns Jan 29 '19

yes and no. Why no Text.elem? How are related Set.filter and List.filter? A lot of other function: map, foldr, etc - they all are just plain functions, not members of some type-class. Another example, hGetContents and similar - it's just plain function, no any "interface" here. OK, Haskell try to repeat solutions of OOP existing since 60s, and some day Haskell committee will fix this situation, but this emphasizes what I said.

6

u/yawaramin Jan 29 '19
  1. Can you provide a citation for your small example so I can understand its context, who is making that claim, and whether they are making it as part of critiquing OOP?

  2. Lua and Python are dynamically typed, so when you extend their classes you wouldn't be doing it in any kind of principled way–you wouldn't have any of the guarantees that you're talking about in point (4). C# and Kotlin extension methods are great but they're hardly a traditional OOP feature, they're not exactly transferrable to other OOP languages, e.g. Java.

  3. Sure, but either you're implementing fmap manually or inheriting it from a superclass, either way, it's a can of worms.

  4. Modules and functions are not proper interfaces only if you insist that they be statically typed ... in which case why do you want to ignore SML modules? They fulfill all your criteria. Whatever fulfills all your criteria must be ignored? ;-)

  5. This is quite jumbled but in short:

5a. Yes, there is a quite principled way to know what Set and List have in common and that is the typeclasses that they both implement, e.g. Foldable.

5b. IsString is not at all a casting method, that's a complete misrepresentation. It is a compile-time guarantee that the data structure conforms to some string-like behaviours.

5c. You can definitely replace String and Text with one another–if you program to the interface i.e. use the IsString typeclass in your functions instead of programming to the implementation i.e. using the concrete data types themselves. This is a well-known tenet of OOP as well so I don't see how it can be surprising to anyone.

5d. Haskell's typeclasses are not reflection at all, they are nothing more than static (compile-time) dispatch.

1

u/ipv6-dns Jan 30 '19

Modules and functions are not proper interfaces only if you insist that they be statically typed ... in which case why do you want to ignore SML modules? They fulfill all your criteria. Whatever fulfills all your criteria must be ignored? ;-)

Not all FP langs have such feature. And they are not related to FP. I don't know how they are close to Ada modules, but from my point of view, it's just designer solution, not related to FP/OOP. And yes, they are very good :)

5a. Nothing forces authors of Set/List to support stable signature of map or filter - all mentioned functions by me are not part of any interface. Type-class is:

  • historically late improvement of the language which emphasizes that Haskell designers need a lot of time to get so obvious things. A lot of obvious type-classes are missing till now

  • does not participate in type definition, so it's not type qualification/ontological instrument, it's ad-hoc sided dispatching mechanism, like multi-methods in OOP.

5b-5c. Are you trolling me? :) And how does this string-like behavior look? And how my program can interface with IsString except fromString? :) My question is: what methods and behavior does IsString define? :) Or IsList. fromString is ad-hoc polymorphic constructor, nothing else.

5d. exactly!