动态语言的用途

发布于 2024-07-13 02:59:26 字数 1431 浏览 11 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

箹锭⒈辈孓 2024-07-20 02:59:26

从理论上讲,没有什么是动态语言能做而静态语言不能做的。 聪明的人投入了大量的精力来开发非常好的动态语言,导致人们现在认为动态语言处于领先地位,而静态语言需要迎头赶上。

随着时间的推移,这种情况将会向另一个方向转变。 各种静态语言已经有:

  • 泛型,它让静态类型在传递对象时选择正确的类型,从而使静态类型变得不那么愚蠢,从而使程序员不必自己进行强制转换

  • 类型推断,这样就不必浪费时间编写应该显而易见的内容

  • 闭包,其中许多有助于将机制与意图分开,让您可以将复杂的算法从大多数现有的成分中组合在一起。< /p>

  • 隐式转换,可让您模拟“猴子修补”,而不会产生通常涉及的风险。

  • 代码加载和对编译器的轻松编程访问,因此用户和第三方可以编写您的程序。 谨慎使用!

  • 更有利于在其中创建领域特定语言的语法。

...毫无疑问还会有更多。 动态运动催生了静态语言设计的一些有趣的发展,我们都从竞争中受益。 我只希望更多这些功能能够成为主流。

有一个地方我没有看到占主导地位的动态语言被取代,那就是浏览器中的 Javascript。 现有的市场太多需要替代,因此重点似乎是让 Javascript 本身变得更好。

In theory, there's nothing that dynamic languages can do and static languages can't. Smart people put a lot of work into making very good dynamic languages, leading to a perception at the moment that dynamic languages are ahead while static ones need to catch up.

In time, this will swing the other way. Already various static languages have:

  • Generics, which make static types less stupid by letting it select the right type when objects are passed around, saving the programmer from having to cast it themselves

  • Type inference, which saves having to waste time on writing the stuff that should be obvious

  • Closures, which among many other things help to separate mechanism from intention, letting you pull together complicated algorithms from mostly existing ingredients.

  • Implicit conversions, which lets you simulate "monkey patching" without the risks it usually involves.

  • Code loading and easy programmatic access to the compiler, so users and third parties can script your program. Use with caution!

  • Syntaxes that are more conducive to the creation of Domain Specific Languages within them.

...and no doubt more to come. The dynamic movement has spawned some interesting developments in static language design, and we all benefit from the competition. I only hope more of these features make it to the mainstream.

There's one place where I don't see the dominant dynamic language being replaced, and that's Javascript in the browser. There's just too much of an existing market to replace, so the emphasis seems to be towards making Javascript itself better instead.

韵柒 2024-07-20 02:59:26

Steve Yegge 介绍了该主题。

Guido van Rossum 在 他对 Scala 的看法中也链接到了该演讲。

Here's Steve Yegge on the subject.

Guido van Rossum also linked to that talk in his take of Scala.

蓝眸 2024-07-20 02:59:26

“我很好奇有什么好处
动态语言,即使你有
那些。”

与 D 编程语言相比:

  • Python 是一种更紧凑的语言。它允许您表达与 D 一样多的内容,但它使用更少的不同概念来实现它 - 少即是多 .

  • Python 强大的标准库 - 包含电池

我不知道 D 是否有交互式提示,但在 Python 中。诸如 ipython 之类的交互式 shell 是开发过程的一个集成部分。

"I'm curious what the benefits are of
dynamic languages even when you have
those."

Compared to D programming language:

  • Python is a more compact language. It allows you to express as much as D but it uses many fewer different concepts to achieve it -- less is more.

  • Python has a powerful standard library -- batteries included.

I don't know whether D has interactive prompts but in Python an interactive shell such as ipython is an integrated part of development process.

爱你不解释 2024-07-20 02:59:26

Python 示例:

def lengths(sequence):
    try:
        return sum(len(item) for item in sequence)
    except TypeError:
        return "Wolf among the sheep!"

>>> lengths(["a", "b", "c", (1, 2, 3)])
6
>>> lengths( ("1", "2", 3) )
'Wolf among the sheep!'

你认为我写这个花了多长时间,以及多少个编译-运行-调试周期?

如果您认为我的示例微不足道,我可以回答说动态语言使许多编程任务变得微不足道。

Example in Python:

def lengths(sequence):
    try:
        return sum(len(item) for item in sequence)
    except TypeError:
        return "Wolf among the sheep!"

>>> lengths(["a", "b", "c", (1, 2, 3)])
6
>>> lengths( ("1", "2", 3) )
'Wolf among the sheep!'

How long do you think this took me to write, and how many compile-run-debug cycles?

If you think my example is trivial, I can reply by saying that dynamic languages make trivial many programming tasks.

温折酒 2024-07-20 02:59:26

在动态语言中,您可以以您知道正确的方式使用值。 在静态类型语言中,您只能以编译器知道正确的方式使用值。 您需要您提到的所有东西来重新获得被类型系统剥夺的灵活性(我不是在抨击静态类型系统,灵活性通常是有充分理由被剥夺的)。 如果您想以语言设计者没有预料到的方式使用值(例如,将不同类型的值放入哈希表中),那么在动态语言中您不必处理很多复杂性。

所以并不是说你不能用静态类型语言来做这些事情(如果你有运行时反射),它只是更复杂。

In dynamic languages you can use values in ways that you know are correct. In a statically typed language you can only use values in ways the compiler knows are correct. You need all of the things you mentioned to regain flexibility that's taken away by the type system (I'm not bashing static type systems, the flexibility is often taken away for good reasons). This is a lot of complexity that you don't have to deal with in a dynamic language if you want to use values in ways the language designer didn't anticipate (for example, putting values of different types in a hash table).

So it's not that you can't do these things in a statically typed language (if you have runtime reflection), it's just more complicated.

用心笑 2024-07-20 02:59:26

我实际上写了一篇关于此的博客文章: linky。 但这篇文章基本上可以这样总结:

您会惊讶地发现,不必在编译时命名变量的类型,您的大脑会承受多大的负担。 因此,Python 往往是一种非常高效的语言。

另一方面,即使有良好的单元测试,您也会对自己犯下的愚蠢错误感到惊讶。

I actually wrote a blog post on this: linky. But that post basically can be summed up like this:

You'd be surprised at how much of a load off your mind it is to not have to name at compile time what type your variable is. Thus, python tends to be a very productive language.

On the other hand, even with good unit tests, you'd also be surprised at what kinds of stupid mistakes you're allowing yourself to make.

终陌 2024-07-20 02:59:26

使用对象时动态类型的一大优点是,当您希望多个类具有相同的接口时,您不再需要使用类层次结构 - 这或多或少就是所谓的鸭子类型。 错误的继承之后很难修复——这使得重构通常比 Python 这样的语言更困难。

One big advantage of dynamic typing when using objects is that you don't need to use class hierarchies anymore when you want several classes to have the same interface - that's more or less what is called duck typing. Bad inheritance is very difficult to fix afterwards - this makes refactoring often harder than it is in a language like python.

一曲爱恨情仇 2024-07-20 02:59:26

关键是,在动态语言中,您可以比静态类型语言更快地实现相同的功能。 因此,生产率通常要高得多。

原则上,模板或多态性之类的东西为您提供了很大的灵活性,但您必须编写大量代码才能使其工作。 在动态语言中,这种灵活性几乎是免费的。

所以我认为你以错误的方式看待差异,生产力确实是这里的要点(就像垃圾收集可以提高生产力,但否则并不能真正让你做新的事情)。

The point is that in a dynamic language you can implement the same functionality much quicker than in a statically typed one. Therefore the productivity is typically much higher.

Things like templates or polymorphism in principle give you lots of flexibility, but you have to write a large amount of code to make it work. In a dynamic language this flexibility almost comes for free.

So I think you look at the difference in the wrong way, productivity really is the main point here (just like garbage collection improves productivity, but otherwise does not really allow you to do new things).

×眷恋的温暖 2024-07-20 02:59:26

使用动态语言,拥有命令行解释器会更容易,因此您可以在命令行上测试内容,而不必担心编译步骤来查看它们是否有效。

With a dynamic language it's much easier to have a command line interpreter so you can test things on the command line and don't have to worry about a compile step to see if they work.

愚人国度 2024-07-20 02:59:26

我发现 Perl 等动态语言以及 Python(在较小程度上)允许我为我需要做的事情编写快速而肮脏的脚本。 动态语言的运行周期要短得多,并且通常比静态类型语言需要编写的代码更少,这提高了我的工作效率。 不幸的是,这是以可维护性为代价的,但这是我用动态语言而不是语言本身编写程序的方式的错误。

I find dynamic languages like Perl and to a lesser extent Python allow me to write quick and dirty scripts for things I need to do. The run cycle is much shorter in dynamic languages and often less code needs to be written then in a statically typed language which increases my productivity. This unfortunately comes at the cost of maintainability but that is a fault of the way I write programs in dynamic languages not in the languages them selves.

蒲公英的约定 2024-07-20 02:59:26

我本来想说关闭,但发现这个帖子...(并不是说我理解它如何在“静态”语言中工作)

相关概念是 函数作为第一类对象高阶过程。 (例如,将函数作为输入和/或返回函数作为输出的函数)

编辑:(对于这里的挑剔者)我将回应我在@David Locke 的帖子上发表的评论。 动态解释语言使得可以将现有的软件程序/项目与临时创建的小函数或类结合使用,以交互方式探索某些内容。 最好的例子可能是函数图。 如果我用 graph(f,xmin,xmax) 函数编写一个函数图形对象,我可以用它来探索像 x2 或 sin(x) 或任何。 我一直在 MATLAB 中这样做; 它被解释并具有匿名函数 (@(x) x^2),可以在解释器提示符下构造这些函数以传递到高阶函数(图形函数、导数运算符、根查找器等)。

I was going to say closures but found this thread... (not that I understand how it would work in a "static" language)

Related concepts are functions-as-first-class-objects and higher-order procedures. (e.g. a function that takes a function as input and/or returns a function as output)

edit: (for the nitpickers here) I'll echo a comment I made on @David Locke's post. Dynamically-interpreted languages make it possible to use an existing software program/project in conjunction with a small function or class created at the spur-of-the-moment to explore something interactively. Probably the best example is function graphing. If I wrote a function-graphing object with a graph(f,xmin,xmax) function, I could use it to explore functions like x2 or sin(x) or whatever. I do this in MATLAB all the time; it's interpreted and has anonymous functions (@(x) x^2) that can be constructed at the interpreter prompt to pass into higher-order functions (graphing functions, derivative operators, root finders, etc).

就此别过 2024-07-20 02:59:26

看看这个 JavaScript 中的 e4x 示例:

var sales = <sales vendor="John">
    <item type="peas" price="4" quantity="6"/>
    <item type="carrot" price="3" quantity="10"/>
    <item type="chips" price="5" quantity="3"/>
  </sales>;

alert( sales.item.(@type == "carrot").@quantity );
alert( sales.@vendor );
for each( var price in sales..@price ) {
  alert( price );
}

,看一下行:

alert( sales.item.(@type == "carrot").@quantity );

特别是 在静态语言中,您不需要编写 sales.item,因为直到运行时您才能知道该 item 是 sales 的属性。
这不限于 e4x。 在编写 SOAP 客户端或任何其他直到运行时才知道的底层类型时,您可以在连接时以类似的风格进行编程。
在静态语言中,您通常需要运行一个工具来以非常详细的方式生成存根类或程序。 然后,如果 Web 服务发生变化,您需要重新生成存根。 看一下 java DOM 代码:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Foo {

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "root" );

        Element author1 = root.addElement( "author" )
            .addAttribute( "name", "James" )
            .addAttribute( "location", "UK" )
            .addText( "James Strachan" );

        Element author2 = root.addElement( "author" )
            .addAttribute( "name", "Bob" )
            .addAttribute( "location", "US" )
            .addText( "Bob McWhirter" );

        return document;
    }
}

绝对比动态代码冗长得多。 当然,它不是静态类型的。 在运行时之前,无法检查您是否将“author”拼写为“autor”。 所有这些冗长的内容本质上是为了让您以静态风格捕捉本质上动态的东西。

我认为这是动态语言的优点之一。

Take a look at this e4x example in JavaScript:

var sales = <sales vendor="John">
    <item type="peas" price="4" quantity="6"/>
    <item type="carrot" price="3" quantity="10"/>
    <item type="chips" price="5" quantity="3"/>
  </sales>;

alert( sales.item.(@type == "carrot").@quantity );
alert( sales.@vendor );
for each( var price in sales..@price ) {
  alert( price );
}

Especially, take a look at line:

alert( sales.item.(@type == "carrot").@quantity );

In typical static languages, you don’t get to write sales.item, since you can not know that item is property of sales until runtime.
This is not limited to e4x. You get to program in similar style when connecting when writing SOAP clients or any other underlying type you do not know until runtime.
In a static language, you would typically need to run a tool that will generate stub classes or program in a very verbose way. Then, if something changes in a web service, you need to regenerate stubs all over again. Take a look at java DOM code:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Foo {

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "root" );

        Element author1 = root.addElement( "author" )
            .addAttribute( "name", "James" )
            .addAttribute( "location", "UK" )
            .addText( "James Strachan" );

        Element author2 = root.addElement( "author" )
            .addAttribute( "name", "Bob" )
            .addAttribute( "location", "US" )
            .addText( "Bob McWhirter" );

        return document;
    }
}

Definitely much more verbose than your dynamic code. And, of course, it is not statically typed. There is no way to check that you misspelled “author” as "autor" until runtime. All this verbosity is essentially there to let you capture something that is dynamic in nature in static style.

I think this is one of the strong points of dynamic languages.

心欲静而疯不止 2024-07-20 02:59:26

当效率和类型安全是优先考虑的时候,倾向于使用编译语言。 否则我想不出任何人不使用 ruby​​ 的理由:)

Compiled languages tend to be used when efficiency and type safety are the priorities. Otherwise I can't think of any reason why anyone wouldn't be using ruby :)

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