首先了解一下基本概念
Program Errors
trapped 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。有了上面的概念,再讨论强、弱类型,静态、动态类型
偏向于不容忍隐式类型转换,弱类型相对于强类型来说类型检查更不严格,比如说允许变量类型的隐式转换,允许强制类型转换等等。强类型语言一般不允许这么做。
弱类型 容忍类型变化
> "1"+2 '12'强类型
>>> "1"+2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects其中静态类型可以分为两种:
如果类型是语言语法的一部分,在是explicitly typed显式类型,比如java和c;如果类型通过编译时推导,是implicity typed隐式类型, 比如ML和Haskell简单地说,就是在声明了一个变量之后,不能改变它的类型的语言,是静态语言;能够随时改变它的类型的语言,是动态语言。因为动态语言的特性,一般需要运行时虚拟机支持。
动态类型
>>> a = 1 >>> type(a) <type 'int'> >>> a = "s" >>> type(a) <type 'str'>静态类型
Prelude> let a = "123" :: Int <interactive>:2:9: Couldn't match expected type `Int' with actual type `[Char]' In the expression: "123" :: Int In an equation for `a': a = "123" :: Int一些图示
