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

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


当前回答

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

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

前女友。 int x; X = 10;

System.out.println (x);

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

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

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

其他回答

摘自Artima的《Typing: Strong vs. Weak, Static vs. Dynamic》文章:

强类型可以防止在不匹配的类型之间混合操作。为了混合类型,必须使用显式转换 弱类型意味着可以在没有显式转换的情况下混合类型

在Pascal Costanza的论文《动态与静态类型——基于模式的分析》(PDF)中,他声称在某些情况下,静态类型比动态类型更容易出错。一些静态类型语言强迫你手动模拟动态类型,以便做“正确的事情”。Lambda the Ultimate讨论过这个问题。

静态类型系统寻求静态地消除某些错误,在不运行程序的情况下检查程序,并试图证明在某些方面的合理性。一些类型系统能够捕获比其他类型更多的错误。例如,如果使用得当,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实际上被编译为一种固有的动态中间语言,这一事实应该表明,在大多数情况下,动态类型并不是一些人认为的巨大性能杀手。

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支持和静态代码分析器。 在每次代码更改之后,您都会对代码更加自信。用这样的工具来维护蛋糕是和平的。

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

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

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

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

前女友。 int x; X = 10;

System.out.println (x);

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

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

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