为什么大多数脚本语言都是松散键入的? [英] Why are most scripting languages loosely typed?

查看:145
本文介绍了为什么大多数脚本语言都是松散键入的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

javascript,python等?

解决方案

首先,您的术语存在一些问题.没有松散类型的语言"这样的东西,脚本语言"这样的术语也含糊不清,最常见的是所谓的动态编程语言".

关于不同类型(例如,如果1 + "2"产生3或错误)的区分方式,有弱键入强键入.

相对于动态键入静态键入,这是关于何时确定类型信息-运行时或运行前.

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

但是,什么是动态语言?我想通过一个人对它们的定义来定义它们.

使用动态语言,您希望快速原型化程序并以某种方式使其工作.您不想做的是正式指定程序的行为,而只是希望它的行为像预期的那样.

因此,如果你写

foo = greatFunction(42)
foo.run()

使用脚本语言,您仅假设有一些greatFunction带有一个数字,该数字将返回您可以run的某些对象.您不会以任何方式为编译器证明这一点-没有预先确定的类型,没有IRunnable ....这会自动使您进入动态键入领域.

但是也有类型推断.类型推断意味着使用静态类型语言,编译器会自动为您找出类型.生成的代码可以非常简洁,但仍为静态类型.举个例子

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

在Haskell中. Haskell会预先弄清这里涉及的所有类型.我们有list是数字列表,map是一些将其他功能应用于列表中任何元素的函数,以及square是从另一数字列表中生成数字列表的.

尽管如此,编译器可以证明所有事情都可以预先解决-正式指定了所有支持的操作.因此,尽管它可以达到类似的表达水平(如果不是更多的话!),我永远不会将Haskell称为脚本语言.


总而言之,脚本语言动态类型的,因为它允许您在不指定的情况下对正在运行的系统进行原型设计,但是每指定一个 存在涉及的操作,这就是脚本语言的用途.

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

javascript , python , etc ?

解决方案

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.

这篇关于为什么大多数脚本语言都是松散键入的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆