programming-languages - 动态类型语言与静态类型语言

标签 programming-languages dynamic-languages type-systems

与静态类型语言相比,动态类型语言有哪些优点和局限性?

另请参阅:whats with the love of dynamic languages (一个更具争议性的话题......)

最佳答案

解释器推断类型和类型转换的能力使开发时间更快,但它也可能引发运行时故障,而在静态类型语言中,您无法在编译时捕获它们。但哪个更好(或者即使这总是正确的)这些天(并且已经很长一段时间)在社区中引起了热烈的讨论。

对这个问题的一个很好的看法来自 Static Typing Where Possible, Dynamic Typing When Needed: The End of the Cold War Between Programming Languages作者:Microsoft 的 Erik Meijer 和 Peter Drayton:

Advocates of static typing argue that the advantages of static typing include earlier detection of programming mistakes (e.g. preventing adding an integer to a boolean), better documentation in the form of type signatures (e.g. incorporating number and types of arguments when resolving names), more opportunities for compiler optimizations (e.g. replacing virtual calls by direct calls when the exact type of the receiver is known statically), increased runtime efficiency (e.g. not all values need to carry a dynamic type), and a better design time developer experience (e.g. knowing the type of the receiver, the IDE can present a drop-down menu of all applicable members). Static typing fanatics try to make us believe that “well-typed programs cannot go wrong”. While this certainly sounds impressive, it is a rather vacuous statement. Static type checking is a compile-time abstraction of the runtime behavior of your program, and hence it is necessarily only partially sound and incomplete. This means that programs can still go wrong because of properties that are not tracked by the type-checker, and that there are programs that while they cannot go wrong cannot be type-checked. The impulse for making static typing less partial and more complete causes type systems to become overly complicated and exotic as witnessed by concepts such as “phantom types” [11] and “wobbly types” [10]. This is like trying to run a marathon with a ball and chain tied to your leg and triumphantly shouting that you nearly made it even though you bailed out after the first mile.

Advocates of dynamically typed languages argue that static typing is too rigid, and that the softness of dynamically languages makes them ideally suited for prototyping systems with changing or unknown requirements, or that interact with other systems that change unpredictably (data and application integration). Of course, dynamically typed languages are indispensable for dealing with truly dynamic program behavior such as method interception, dynamic loading, mobile code, runtime reflection, etc. In the mother of all papers on scripting [16], John Ousterhout argues that statically typed systems programming languages make code less reusable, more verbose, not more safe, and less expressive than dynamically typed scripting languages. This argument is parroted literally by many proponents of dynamically typed scripting languages. We argue that this is a fallacy and falls into the same category as arguing that the essence of declarative programming is eliminating assignment. Or as John Hughes says [8], it is a logical impossibility to make a language more powerful by omitting features. Defending the fact that delaying all type-checking to runtime is a good thing, is playing ostrich tactics with the fact that errors should be caught as early in the development process as possible.

关于programming-languages - 动态类型语言与静态类型语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/125367/

相关文章:

c# - 在什么假设下 Re-Sharper 会向我显示 "Use implicitly typed local variable"消息?

java - 动态语言中的 “dynamic” 会不会太多?

scala - 为什么在 Scala 中使用类型格?

c# - C# 是单分派(dispatch)语言还是多分派(dispatch)语言?

python - 从解释到 native 代码 : "dynamic" languages compiler support

python - Lisp vs Python——静态编译

JavaFX:为什么无论我传递给 `changeListener`什么样的 `textArea.getKeyPressedProperty().addListener`都无法编译

haskell - 将固定长度向量函数应用于较长的固定长度向量的初始部分

encryption - 有谁知道这是什么语言的吗?

variables - 哪些语言有交换变量的原始操作?