为什么大多数脚本语言都是松散类型的?

发布于 2024-10-04 07:48:48 字数 56 浏览 9 评论 0原文

为什么大多数脚本语言都是松散类型的?例如

javascript、python 等?

why most of the scripting languages are loosely typed ? for example

javascript , python , etc ?

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

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

发布评论

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

评论(2

无风消散 2024-10-11 07:48:48

首先,您的术语存在一些问题。不存在松散类型语言,术语脚本语言也很模糊,最常见的是所谓的动态编程语言

关于区分不同类型的严格程度,存在弱类型强类型之分(即,如果 1 + "2" 产生 3 或错误)。

还有动态静态类型,这是关于何时确定类型信息的——运行时或之前。

那么现在,什么是动态语言?一种解释而不是编译的语言? 当然不是,因为语言的运行方式从来都不是该语言的某些固有特征,而是纯粹的实现细节。事实上,同一种语言可以有解释器和编译器。 Haskell 有 GHC 和 GHCi,甚至 C 也有 Ch 解释器。

那么,什么是动态语言呢?我想通过人们如何使用它们来定义它们。

在动态语言中,您喜欢快速构建程序原型并使其以某种方式运行。您不想做的是正式指定程序的行为,您只是希望它按照预期运行。

因此,如果您

foo = greatFunction(42)
foo.run()

使用脚本语言编写,您将简单地假设有一些greatFunction接受一个数字,该数字将返回一些您可以运行。您不以任何方式向编译器证明这一点 - 没有预先确定的类型,没有 IRunnable ...。这会自动让您进入动态类型领域。

但也有类型推断。类型推断意味着在静态类型语言中,编译器会自动为您找出类型。生成的代码可以非常简洁,但仍然是静态类型的。 以 Haskell为例

square list = map (\x -> x * x) list

。 Haskell 提前计算出这里涉及的所有类型。我们有 list 是一个数字列表,map 是一些将其他函数应用于列表中任何元素的函数,以及 square 生成一个列表另一个数字列表中的数字。

尽管如此,编译器可以提前证明一切正常 - 任何支持的操作都被正式指定。因此,我永远不会称 Haskell 为脚本语言,尽管它可以达到类似的表达水平(如果不是更高!)。


总而言之,脚本语言动态类型,因为这允许您在不指定的情况下构建运行系统的原型,但假设涉及的每个操作都存在,这就是脚本语言的用途。

First of all, there are some issues with your terminology. There is no such thing as a loosely typed language and the term scripting language is vague too, most commonly referring to so called dynamic programming languges.

There is weak typing vs. strong typing about how rigorously is distinguished between different types (i.e. if 1 + "2" yields 3 or an error).

And there is dynamic vs. static typing, which is about when type information is determined - while or before running.

So now, what is a dynamic language? A language that is interpreted instead of compiled? Surely not, since the way a language is run is never some inherent characteristic of the language, but a pure implementation detail. In fact, there can be interpreters and compilers for one-and-the-same language. There is GHC and GHCi for Haskell, even C has the Ch interpreter.

But then, what are dynamic languges? I'd like to define them through how one works with them.

In a dynamic language, you like to rapidly prototype your program and just get it work somehow. What you don't want to do is formally specifying the behaviour of your programs, you just want it to behave like intended.

Thus if you write

foo = greatFunction(42)
foo.run()

in a scripting language, you'll simply assume that there is some greatFunction taking a number that will returns some object you can run. You don't prove this for the compiler in any way - no predetmined types, no IRunnable ... . This automatically gets you in the domain of dynamic typing.

But there is type inference too. Type inference means that in a statically-typed language, the compiler does automatically figure out the types for you. The resulting code can be extremely concise but is still statically typed. Take for example

square list = map (\x -> x * x) list

in Haskell. Haskell figures out all types involved here in advance. We have list being a list of numbers, map some function that applies some other function to any element of a list and square that produces a list of numbers from another list of numbers.

Nonetheless, the compiler can prove that everything works out in advance - the operations anything supports are formally specified. Hence, I'd never call Haskell a scripting language though it can reach similar levels of expressiveness (if not more!).


So all in all, scripting languages are dynamically typed because that allows you to prototype a running system without specifying, but assuming every single operation involved exists, which is what scripting languages are used for.

绮烟 2024-10-11 07:48:48

我不太明白你的问题。除了 PHP、VBScript、COMMAND.COM 和 Unix shell 之外,我实在想不出任何松散类型的脚本语言。

非松散类型脚本语言的一些示例包括 Python、Ruby、Mondrian、JavaFXScript、PowerShell、Haskell、Scala、ELisp、Scheme、AutoLisp、Io、Ioke、Seph、Groovy、Fantom、Boo、Cobra、Guile、Slate、Smalltalk 、Perl、...

I don't quite understand your question. Apart from PHP, VBScript, COMMAND.COM and the Unix shell(s) I can't really think of any loosely typed scripting languages.

Some examples of scripting languages which are not loosely typed are Python, Ruby, Mondrian, JavaFXScript, PowerShell, Haskell, Scala, ELisp, Scheme, AutoLisp, Io, Ioke, Seph, Groovy, Fantom, Boo, Cobra, Guile, Slate, Smalltalk, Perl, …

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