为什么大多数脚本语言都是松散类型的?
为什么大多数脚本语言都是松散类型的?例如
javascript、python 等?
why most of the scripting languages are loosely typed ? for example
javascript , python , etc ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的术语存在一些问题。不存在松散类型语言,术语脚本语言也很模糊,最常见的是所谓的动态编程语言。
关于区分不同类型的严格程度,存在弱类型与强类型之分(即,如果
1 + "2"
产生3
或错误)。还有动态与静态类型,这是关于何时确定类型信息的——运行时或之前。
那么现在,什么是动态语言?一种解释而不是编译的语言? 当然不是,因为语言的运行方式从来都不是该语言的某些固有特征,而是纯粹的实现细节。事实上,同一种语言可以有解释器和编译器。 Haskell 有 GHC 和 GHCi,甚至 C 也有 Ch 解释器。
那么,什么是动态语言呢?我想通过人们如何使用它们来定义它们。
在动态语言中,您喜欢快速构建程序原型并使其以某种方式运行。您不想做的是正式指定程序的行为,您只是希望它按照预期运行。
因此,如果您
使用脚本语言编写,您将简单地假设有一些
greatFunction
接受一个数字,该数字将返回一些您可以运行。您不以任何方式向编译器证明这一点 - 没有预先确定的类型,没有
IRunnable
...。这会自动让您进入动态类型领域。但也有类型推断。类型推断意味着在静态类型语言中,编译器会自动为您找出类型。生成的代码可以非常简洁,但仍然是静态类型的。 以 Haskell为例
。 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"
yields3
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
in a scripting language, you'll simply assume that there is some
greatFunction
taking a number that will returns some object you canrun
. You don't prove this for the compiler in any way - no predetmined types, noIRunnable
... . 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
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 andsquare
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.
我不太明白你的问题。除了 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, …