静态语言、动态语言

来源:互联网 发布:seo内链 编辑:程序博客网 时间:2024/04/29 20:01

一、
前两者,弱/强类型指的是语言类型系统的类型检查的严格程度。后两者指的是变量与类型的绑定方法。

弱类型相对于强类型来说类型检查更不严格,比如说允许变量类型的隐式转换,允许强制类型转换等等。强类型语言一般不允许这么做。

静态类型指的是编译器在compile time执行类型检查,动态类型指的是编译器(虚拟机)在runtime执行类型检查。简单地说,在声明了一个变量之后,不能改变它的类型的语言,是静态语言;能够随时改变它的类型的语言,是动态语言。因为动态语言的特性,一般需要运行时虚拟机支持。

二、
1.
Program Error
strapped errors:导致程序终止执行,如除0,Java中数组越界访问
untrapped errors: 出错后继续执行,但可能出现任意行为。如C里的缓冲区溢出、Jump到错误地址Forbidden Behaviours 语言设计时,可以定义一组forbidden behaviors. 它必须包括所有untrapped errors, 但可能包含trapped errors.
Well behaved、ill behaved
well behaved: 如果程序执行不可能出现forbidden behaviors, 则为well behaved。
ill behaved: 否则为ill behaved…

2.有了上面的概念,再讨论强、弱类型,静态、动态类型强、弱类型

强类型 strongly typed: 如果一种语言的所有程序都是well behaved——即不可能出现forbidden behaviors,则该语言为strongly typed。
弱类型 weakly typed: 否则为weakly typed。比如C语言的缓冲区溢出,属于trapped errors,即属于forbidden behaviors..故C是弱类型
前面的人也说了,弱类型语言,类型检查更不严格,如偏向于容忍隐式类型转换。譬如说C语言的int可以变成double。 这样的结果是:容易产生forbidden behaviours,所以是弱类型的
动态、静态类型
静态类型 statically: 如果在编译时拒绝ill behaved程序,则是statically typed;
动态类型dynamiclly: 如果在运行时拒绝ill behaviors, 则是dynamiclly typed。

3.
这里写图片描述
红色区域外:well behaved (type soundness)
红色区域内:ill behaved
如果所有程序都是灰的,strongly typed
否则如果存在红色的程序,weakly typed
编译时排除红色程序,statically typed
运行时排除红色程序,dynamically typed
所有程序都在黄框以外,type safe


参考:《Type Systems》 Luca Cardelli - Microsoft Research

Trapped error: An execution error that immediately results in a fault.

Untrapped error: An execution error that does not immediately result in
a fault.

Forbidden error: The occurrence of one of a predetermined class of execution errors;Typically the improper application of an operation to a value, such as not(3).

Well behaved: A program fragment that will not produce forbidden errors at run time.

Strongly checked language: A language where no forbidden errors can occur at run time (depending on the definition of forbidden error).

Weakly checked language: A language that is statically checked but provides no clear guarantee of absence of execution errors.

Statically checked language: A language where good behavior is
determined before execution.

Dynamically checked language: A language where good behavior is
enforced during execution.

Type safety: The property stating that programs do
not cause untrapped errors.

Explicitly typed language: A typed language where types are part of
the syntax.

Implicitly typed language: A typed language where types are not part of the
syntax.

这里写图片描述

原文参考

原创粉丝点击