什么使编程语言具有动态性?

发布于 2024-10-16 05:58:51 字数 68 浏览 3 评论 0原文

什么样的编程语言才可以称为动态语言?我应该使用动态编程语言来解决什么样的问题?静态编程语言和动态编程语言的主要区别是什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

◇流星雨 2024-10-23 05:58:51

我不认为这里有黑白之分——动态和静态之间有一个完整的范围。

让我们为光谱的每一方举两个极端的例子,看看我们会走向何方。

Haskell 是静态方向的一个极端。

  • 它有一个强大的类型系统,可以在编译时进行检查:如果您的程序可以编译,则不会出现常见和不那么常见的错误。
  • 编译后的形式与 haskell 程序有很大不同(它是二进制文件)。因此,运行时反射和修改是很困难的,除非您已经预见到了。与解释原始文件相比,结果可能更有效,因为编译器可以自由地进行时髦的优化。

因此,对于静态语言,我通常认为:需要相当长的编译时分析,类型系统将防止我犯愚蠢的错误,但也会防止我做一些实际上有效的事情,如果我想在运行时对程序进行任何操作,这会有点痛苦,因为程序的运行时表示(即其编译形式)与实际语言本身不同。如果我没有预见到的话,以后修改事情可能会很痛苦。

Clojure 是动态方向的一个极端。

  • 它也有类型系统,但在编译时没有类型检查。许多常见错误只能通过运行程序才能发现。
  • Clojure 程序本质上只是 Clojure 列表(数据结构),并且可以这样进行操作。因此,在进行运行时反射时,您实际上是在或多或少地处理 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.

  • It has a powerful type system that is checked at compile time: If your program compiles it is free from common and not so common errors.
  • The compiled form is very different from the haskell program (it is a binary). Consequently runtime reflection and modification is hard, unless you have foreseen it. In comparison to interpreting the original, the result is potentially more efficient, as the compiler is free to do funky optimizations.

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.

  • It too has a type system, but at compile time there is no type checking. Many common errors can only be discovered by running the program.
  • Clojure programs are essentially just Clojure lists (the data structure) and can be manipulated as such. So when doing runtime reflection, you are actually processing a Clojure program more or less as you would type it - the runtime form is very close to the programming language itself. So you can basically do the same things at runtime as you could at "type time". Consequently, runtime performance may suffer because the compiler can't do many up-front optimizations.

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.

假装不在乎 2024-10-23 05:58:51

动态语言在运行时执行许多任务,而静态语言则在编译时执行这些任务。
所讨论的任务通常是以下一项或多项:类型系统、方法分派和代码生成。

这也几乎回答了有关其用法的问题。

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.

时光是把杀猪刀 2024-10-23 05:58:51

使用中存在很多不同的定义,但一个可能的区别是:

  • 动态语言通常使用动态打字。
  • 静态语言通常使用静态类型。

有些语言很难分为静态类型或动态类型。例如,C# 传统上被视为静态类型语言,但 C# 4.0 引入了一种名为 dynamic 其行为在某些方面更像动态类型而不是静态类型。

There are a lot of different definitions in use, but one possible difference is:

  • A dynamic language typically uses dynamic typing.
  • A static language typically uses static typing.

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.

埖埖迣鎅 2024-10-23 05:58:51

什么使编程语言被称为动态语言。

动态语言通常被认为是那些在运行时提供灵活性的语言。请注意,这不一定与静态类型系统冲突。例如,F# 最近在一次会议上被评为“.NET 上最受欢迎的动态语言”,尽管它是静态类型的。许多人认为 F# 是一种动态语言,因为它提供了运行时功能,例如元循环求值、读取-求值-打印循环 (REPL) 和动态类型(某种)。此外,类型推断意味着 F# 代码不会像大多数静态类型语言(例如 C、C++、Java、C# 2、Scala)那样充斥着类型声明。

我应该使用动态语言来解决哪些问题。

一般来说,只要时间和空间不是至关重要的,您可能总是希望使用具有运行时灵活性和运行时编译等功能的语言。

What qualifies a programming language to be called dynamic language.

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).

What are the problems for which I should go for dynamic language to solve.

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.

ゃ懵逼小萝莉 2024-10-23 05:58:51

该线程很好地涵盖了这个问题:

静态/动态与强/弱

This thread covers the issue pretty well:

Static/Dynamic vs Strong/Weak

我很坚强 2024-10-23 05:58:51

这个问题是在动态语言向导系列 - 语言设计小组中提出的(24m 04s)。

Jonathan Rees 的回答:

一看就知道

来自 盖伊·斯蒂尔

动态语言是一种将尽可能多的决策推迟到运行时的语言。

例如关于数组大小、要分配的数据对象的数量等决策。

这个概念推迟到运行时,这就是我所理解的动态。

The question is asked during Dynamic Languages Wizards Series - Panel on Language Design (at 24m 04s).

Answer from Jonathan Rees:

You know one when you see one

Answer from Guy Steele:

A dynamic language is one that defers as many decisions as possible to run time.

For example about array size, the number of data objects to allocate, decisions like that.

The concept is deferring until runtime, that's what I understand to be dynamic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文