什么使编程语言具有动态性?
什么样的编程语言才可以称为动态语言?我应该使用动态编程语言来解决什么样的问题?静态编程语言和动态编程语言的主要区别是什么?
What qualifies a programming language to be called dynamic language? What sort of problems should I use a dynamic programming language to solve? What is the main difference between static programming languages and dynamic programming languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不认为这里有黑白之分——动态和静态之间有一个完整的范围。
让我们为光谱的每一方举两个极端的例子,看看我们会走向何方。
Haskell 是静态方向的一个极端。
因此,对于静态语言,我通常认为:需要相当长的编译时分析,类型系统将防止我犯愚蠢的错误,但也会防止我做一些实际上有效的事情,如果我想在运行时对程序进行任何操作,这会有点痛苦,因为程序的运行时表示(即其编译形式)与实际语言本身不同。如果我没有预见到的话,以后修改事情可能会很痛苦。
Clojure 是动态方向的一个极端。
对于动态语言,我通常认为:编译步骤短(基本上只是读取语法),如此快速和增量开发,实际上对我可以做的事情没有限制,但不会阻止我犯愚蠢的错误。
正如其他帖子所指出的,其他语言尝试采取更多的中间立场 - 例如,F# 和 C# 等静态语言通过单独的 API 提供反射功能,当然也可以通过使用 F# 的 REPL 等聪明的工具来提供增量开发。动态语言有时提供可选的类型(如 Racket、Strongtalk),并且通常似乎有更高级的测试框架来弥补编译时缺乏任何健全性检查的情况。此外,类型提示虽然在编译时不进行检查,但对于生成更高效的代码(例如 Clojure)来说是有用的提示。
如果您正在寻找针对给定问题的正确工具,那么这当然是您可以考虑的维度之一 - 但其本身不太可能迫使您做出任何决定。考虑一下您正在考虑的语言的其他属性 - 它是函数式语言、面向对象语言、逻辑语言还是……语言?它有一个适合我需要的东西的良好框架吗?我是否需要稳定性和二进制向后兼容性,或者我可以忍受编译器中的一些混乱吗?我需要大量的工具吗?等等。
I don't think there is black and white here - there is a whole spectrum between dynamic and static.
Let's take two extreme examples for each side of the spectrum, and see where that takes us.
Haskell is an extreme in the static direction.
So for static languages I usually think: fairly lengthy compile-time analysis needed, type system will prevent me from making silly mistakes but also from doing some things that are actually valid, and if I want to do any manipulation of a program at runtime, it's going to be somewhat of a pain because the runtime representation of a program (i.e. its compiled form) is different from the actual language itself. Also it could be a pain to modify things later on if I have not foreseen it.
Clojure is an extreme in the dynamic direction.
For dynamic languages I usually think: short compilation step (basically just reading syntax), so fast and incremental development, practically no limits to what it will allow me to do, but won't prevent me from silly mistakes.
As other posts have indicated, other languages try to take more of a middle ground - e.g. static languages like F# and C# offer reflection capabilities through a separate API, and of course can offer incremental development by using clever tools like F#'s REPL. Dynamic languages sometimes offer optional typing (like Racket, Strongtalk), and generally, it seems, have more advanced testing frameworks to offset the lack of any sanity checking at compile time. Also type hints, while not checked at compile time, are useful hints to generate more efficient code (e.g. Clojure).
If you are looking to find the right tool for a given problem, then this is certainly one of the dimensions you can look at - but by itself is not likely to force a decision either way. Have a think about the other properties of the languages you are considering - is it a functional or OO or logic or ... language? Does it have a good framework for the things I need? Do I need stability and binary backwards compatibility, or can I live with some churn in the compiler? Do I need extensive tooling?Etc.
动态语言在运行时执行许多任务,而静态语言则在编译时执行这些任务。
所讨论的任务通常是以下一项或多项:类型系统、方法分派和代码生成。
这也几乎回答了有关其用法的问题。
Dynamic language does many tasks at runtime where a static language would do them at compile-time.
The tasks in question are usually one or more of: type system, method dispatch and code generation.
Which also pretty much answers the questions about their usage.
使用中存在很多不同的定义,但一个可能的区别是:
有些语言很难分为静态类型或动态类型。例如,C# 传统上被视为静态类型语言,但 C# 4.0 引入了一种名为
dynamic
其行为在某些方面更像动态类型而不是静态类型。There are a lot of different definitions in use, but one possible difference is:
Some languages are difficult to classify as either static or dynamically typed. For example, C# is traditionally regarded as a statically typed language, but C# 4.0 introduced a static type called
dynamic
which behaves in some ways more like a dynamic type than a static type.动态语言通常被认为是那些在运行时提供灵活性的语言。请注意,这不一定与静态类型系统冲突。例如,F# 最近在一次会议上被评为“.NET 上最受欢迎的动态语言”,尽管它是静态类型的。许多人认为 F# 是一种动态语言,因为它提供了运行时功能,例如元循环求值、读取-求值-打印循环 (REPL) 和动态类型(某种)。此外,类型推断意味着 F# 代码不会像大多数静态类型语言(例如 C、C++、Java、C# 2、Scala)那样充斥着类型声明。
一般来说,只要时间和空间不是至关重要的,您可能总是希望使用具有运行时灵活性和运行时编译等功能的语言。
Dynamic languages are generally considered to be those that offer flexibility at run-time. Note that this does not necessarily conflict with static type systems. For example, F# was recently voted "favorite dynamic language on .NET" at a conference even though it is statically typed. Many people consider F# to be a dynamic language because it offers run-time features like meta-circular evaluation, a Read-Evaluate-Print-Loop (REPL) and dynamic typing (of sorts). Also, type inference means that F# code is not littered with type declarations like most statically typed languages (e.g. C, C++, Java, C# 2, Scala).
In general, provided time and space are not of critical importance you probably always want to use languages with run-time flexibility and capabilities like run-time compilation.
该线程很好地涵盖了这个问题:
静态/动态与强/弱
This thread covers the issue pretty well:
Static/Dynamic vs Strong/Weak
这个问题是在动态语言向导系列 - 语言设计小组中提出的(24m 04s)。
Jonathan Rees 的回答:
来自 盖伊·斯蒂尔:
The question is asked during Dynamic Languages Wizards Series - Panel on Language Design (at 24m 04s).
Answer from Jonathan Rees:
Answer from Guy Steele: