与静态类型语言相比,动态类型语言的优点和局限性是什么?

另见:动态语言的爱是什么(一个更具争议性的话题…)


当前回答

Perhaps the single biggest "benefit" of dynamic typing is the shallower learning curve. There is no type system to learn and no non-trivial syntax for corner cases such as type constraints. That makes dynamic typing accessible to a lot more people and feasible for many people for whom sophisticated static type systems are out of reach. Consequently, dynamic typing has caught on in the contexts of education (e.g. Scheme/Python at MIT) and domain-specific languages for non-programmers (e.g. Mathematica). Dynamic languages have also caught on in niches where they have little or no competition (e.g. Javascript).

最简洁的动态类型语言(例如Perl, APL, J, K, Mathematica)是特定于领域的,并且在它们设计的利基领域中比最简洁的通用静态类型语言(例如OCaml)要简洁得多。

动态类型的主要缺点是:

运行时类型错误。 要达到相同水平的正确性是非常困难的,甚至实际上是不可能的,并且需要大量的测试。 没有经过编译器验证的文档。 糟糕的性能(通常在运行时,但有时在编译时,例如斯大林方案)和由于依赖复杂的优化而不可预测的性能。

就我个人而言,我是伴随着动态语言长大的,但作为一名专业人士,除非没有其他可行的选择,否则不会用40英尺的杆子接触它们。

其他回答

静态类型系统寻求静态地消除某些错误,在不运行程序的情况下检查程序,并试图证明在某些方面的合理性。一些类型系统能够捕获比其他类型更多的错误。例如,如果使用得当,c#可以消除空指针异常,而Java没有这种能力。Twelf有一个类型系统,它实际上保证证明将终止,“解决”停止问题。

However, no type system is perfect. In order to eliminate a particular class of errors, they must also reject certain perfectly valid programs which violate the rules. This is why Twelf doesn't really solve the halting problem, it just avoids it by throwing out a large number of perfectly valid proofs which happen to terminate in odd ways. Likewise, Java's type system rejects Clojure's PersistentVector implementation due to its use of heterogeneous arrays. It works at runtime, but the type system cannot verify it.

因此,大多数类型系统都提供了“转义”,即覆盖静态检查器的方法。对于大多数语言来说,它们采取强制转换的形式,尽管有些语言(如c#和Haskell)有完整的模式被标记为“不安全”。

Subjectively, I like static typing. Implemented properly (hint: not Java), a static type system can be a huge help in weeding out errors before they crash the production system. Dynamically typed languages tend to require more unit testing, which is tedious at the best of times. Also, statically typed languages can have certain features which are either impossible or unsafe in dynamic type systems (implicit conversions spring to mind). It's all a question of requirements and subjective taste. I would no more build the next Eclipse in Ruby than I would attempt to write a backup script in Assembly or patch a kernel using Java.

哦,那些说“打x字比打y字效率高10倍”的人简直是在吹牛。在许多情况下,动态类型可能“感觉”更快,但一旦您真正尝试运行您的花哨应用程序,它就失去了优势。同样地,静态类型可能看起来是完美的安全网,但只要看一下Java中一些更复杂的泛型类型定义,大多数开发人员就会匆忙寻找令人眼花缭乱的东西。即使有类型系统和生产力,也没有什么灵丹妙药。

最后注意:在比较静态类型和动态类型时,不要担心性能。像V8和TraceMonkey这样的现代jit正在危险地接近静态语言性能。此外,Java实际上被编译为一种固有的动态中间语言,这一事实应该表明,在大多数情况下,动态类型并不是一些人认为的巨大性能杀手。

关键是要有合适的工具。这两种方法都不是100%的好方法。这两种制度都是人为创造的,都有缺陷。抱歉,我们做的东西太烂了。

我喜欢动态类型,因为它让我摆脱了我的方式,但是是的,运行时错误可能会出现,这是我没有计划到的。 静态类型可能会修复前面提到的错误,但会让新手(在类型化语言中)程序员疯狂地尝试在常量字符和字符串之间进行强制转换。

静态类型: Java和Scala等语言是静态类型的。

在代码中使用变量之前,必须对变量进行定义和初始化。

前女友。 int x; X = 10;

System.out.println (x);

动态类型: Perl是一种动态类型语言。

变量在代码中使用之前不需要初始化。

y = 10;在后面的代码中使用这个变量

It depends on context. There a lot benefits that are appropriate to dynamic typed system as well as for strong typed. I'm of opinion that the flow of dynamic types language is faster. The dynamic languages are not constrained with class attributes and compiler thinking of what is going on in code. You have some kinda freedom. Furthermore, the dynamic language usually is more expressive and result in less code which is good. Despite of this, it's more error prone which is also questionable and depends more on unit test covering. It's easy prototype with dynamic lang but maintenance may become nightmare.

相对于静态类型系统的主要优点是IDE支持和静态代码分析器。 在每次代码更改之后,您都会对代码更加自信。用这样的工具来维护蛋糕是和平的。

Perhaps the single biggest "benefit" of dynamic typing is the shallower learning curve. There is no type system to learn and no non-trivial syntax for corner cases such as type constraints. That makes dynamic typing accessible to a lot more people and feasible for many people for whom sophisticated static type systems are out of reach. Consequently, dynamic typing has caught on in the contexts of education (e.g. Scheme/Python at MIT) and domain-specific languages for non-programmers (e.g. Mathematica). Dynamic languages have also caught on in niches where they have little or no competition (e.g. Javascript).

最简洁的动态类型语言(例如Perl, APL, J, K, Mathematica)是特定于领域的,并且在它们设计的利基领域中比最简洁的通用静态类型语言(例如OCaml)要简洁得多。

动态类型的主要缺点是:

运行时类型错误。 要达到相同水平的正确性是非常困难的,甚至实际上是不可能的,并且需要大量的测试。 没有经过编译器验证的文档。 糟糕的性能(通常在运行时,但有时在编译时,例如斯大林方案)和由于依赖复杂的优化而不可预测的性能。

就我个人而言,我是伴随着动态语言长大的,但作为一名专业人士,除非没有其他可行的选择,否则不会用40英尺的杆子接触它们。