Functional style object model?

Is it practical to implement an object model in a functional style?

One problem that OOP outperforms describes object models.

For example, the HTML DOM is a complex beast consisting of a beast that interacts directly with the user interface and requires programmability from dynamic languages. OOP functions are generally useful in many ways:

  • User access restrictions provide interaction with untrusted code (e.g. javascript) safe
  • Functions and properties of accessories make binding to the user interface more convenient
  • No need to go around the object model all the time making methods much easier.

The UI side of the story may be a bit of a controversy if you implement the model using MVVM, but you are still constantly struggling with the state inside.

I work in F # for this project, so I could easily resort to OOP, but I am curious how far I can advance it until it becomes impractical. Could there be design templates or something else?

+6
source share
1 answer

It's a little philosophical to have the β€œright” answer, but still I will bite.

In my opinion, the problem arises because you consider that FP and OO should be compared, and it is not. FP and imperative programming are mapped to each other, i.e. using expressions versus using operators.

Part of the problem is that OO does not have a clear definition, in my opinion, anyway. To support this, I would like to point out Alan Kay, who said: "In fact, I coined the term" object oriented, "and I can say that I did not have C ++ in mind." However, in most languages ​​we consider OO, i.e. java / C # take more after C ++ than smalltalk.

What style of OO C ++ / java / C # gives us a good way to organize our code in models, create data, add properties to them, etc. It is practically inoperative and can be used with functional programming.

As you point out, many C ++ / java / C # tend to be restrained, but they should not be, both java and C # have fundamental types, such as their string classes, which are immutable. Its true java and C # do not allow you to create an immutable class, but with little effort you can do it.

What brings us to where it is invariably assigned? In my projects, it usually starts with the fact that everything becomes unchanged, since it makes things easier, and if I see that it causes performance problems, I start adding some variability to critical paths. The only immutability of the place will never work, these are the GUI controls, which usually contain too many states to be unchanged. Having said that, you can build the GUI for quite some time using an immutable "combinatorial" approach, then it is interpreted using mutable gui controls. This is more or less what WebSharper guys do: http://www.intellifactory.com/products/wsp/Home.aspx

Another great resource for discussing FP / OO is brains. "How does functional programming affect the structure of your code?" (which greatly influenced my thinking about FP / OO): http://lorgonblog.wordpress.com/2008/09/22/how-does-functional-programming-affect-the-structure-of-your-code/

+5
source

Source: https://habr.com/ru/post/886857/


All Articles