开发者

What qualifies a programming language as dynamic?

开发者 https://www.devze.com 2023-02-08 16:13 出处:网络
What qualifies a programming language to be called dynamic language?开发者_StackOverflow社区What sort of problems should I use a dynamic programming language to solve?What is the main difference betwe

What qualifies a programming language to be called dynamic language?开发者_StackOverflow社区 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?


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.


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.


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.


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.


This thread covers the issue pretty well:

Static/Dynamic vs Strong/Weak


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号