为什么可变类会将功能范式变为命令式? [英] Why mutable classes change functional paradigm to imperative?

查看:102
本文介绍了为什么可变类会将功能范式变为命令式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#支持可变类,但是,我读到函数式编程范例不应该包含可变类/对象。如果是这样,那将是必要的。我想知道为什么?

F# supports mutable classes, however, I read that functional programming paradigm should not contain mutable classes/objects. If so, it will be imperative instead. I want to know why?

推荐答案

首先,你不应该错误地比较范式,功能与命令。功能实际上可以被认为是一种必要的。 (这不是声明,对吧?:-))严格来说,为什么这个问题并不正确。为什么?因为它是这样设计的。



这是因为非常纯粹的范例是非常不切实际的。具有不可变类实例的编程范例不能是OOP。尝试开发一个纯粹不可变类的OOP应用程序(它实际上并不意味着类完全不可变,这意味着它们从构造完成的那一刻起是不可变的),甚至是对这样的开发进行成像,你会看到。 br />


F#实际上不是一种功能语言。这是一种多范式语言:面向对象,功能强大,命令式等等:

http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29 [ ^ ]。







使用多范式语言,您可以自由组合某种范式。什么用纯粹的功能方法?没有什么能阻止你创建不可变的类或结构(例如, System.String 是100%不可变的,你可以创建自己的类型。)



这样,您产品的一部分可以是纯粹的功能。对于其他一些部分,您只能找到一些有用的功能特性。其他一些部分可能是非功能性的或OOP +功能性的。语言应该促进良好的风格,但强制执行要少得多,只强制执行,而不是限制代码的表达能力。



说到编译器执行(其中仍然很重要)与个人编程学科相比:你应该明白,即使执行力度最大,也总是可以搞砸一切。



[结束编辑]



为什么?因为它提供了一个很好的实用功能集。因为它是这样设计的。



-SA
First of all, you should not incorrectly compare paradigms, functional vs imperative. Functional can actually be considered as a kind of imperative. (It is not declarative, right? :-)) And the question "why" is not, strictly speaking, correct. Why? Because it was designed this way.

This is because very pure paradigms are very impractical. The programming paradigm with immutable class instances cannot be OOP. Try to develop an OOP application with purely immutable classes (it actually cannot mean that classes are immutable at all, it means that they are immutable from the moment of completion of their construction), or even imaging such development, and you will see.

F# is not really a functional language. This is a multi-paradigm language: object-oriented, functional, imperative, and so on:
http://en.wikipedia.org/wiki/F_Sharp_%28programming_language%29[^].



With a multi-paradigm language, you can freely combine certain paradigm. What to use purely functional approach? Nothing prevents your from creation of immutable classes or structures (for example, System.String is 100% immutable, and you can create your own types like that.)

This way, part of your product can be purely functional. For some other part, you can find only some functional features useful. Some other part can be non-functional or OOP+functional. Language should promote good style, but enforce much less, only what is critically important to enforce, not limiting expressive power of the code.

Speaking of the compiler enforcement (which is still important) vs personal programming discipline: you should understand that even with strongest enforcement, it's always possible to screw up everything.

[END EDIT]

Why? Because it present a good practical feature set. Because it was designed this way.

—SA


这篇关于为什么可变类会将功能范式变为命令式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆