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

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


当前回答

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

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

前女友。 int x; X = 10;

System.out.println (x);

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

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

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

其他回答

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英尺的杆子接触它们。

关于静态语言和动态语言有很多不同的东西。对我来说,主要的区别是在动态语言中,变量没有固定的类型;相反,类型被绑定到值。因此,要执行的确切代码直到运行时才确定。

在早期或naïve实现中,这是一个巨大的性能拖累,但现代jit已经非常接近优化静态编译器的最佳性能。(在一些边缘情况下,甚至比这更好)。

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;在后面的代码中使用这个变量