弱类型、强类型、动态类型、静态类型语言的区别-迷惑笔记

强类型:偏向于不容忍隐式类型转换。譬如说haskell的int就不能变成double

>>> "1"+2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

弱类型:偏向于容忍隐式类型转换。譬如说C语言的int可以变成double

> "1"+2
'12'

静态类型:编译的时候就知道每一个变量的类型,因为类型错误而不能做的事情是语法错误。

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

动态类型:编译的时候不知道每一个变量的类型,因为类型错误而不能做的事情是运行时错误。

>>> a = 1
>>> type(a)
<type 'int'>
>>> a = "s"
>>> type(a)
<type 'str'>

参考资料:

https://en.wikipedia.org/wiki/Type_system

https://www.zhihu.com/people/excited-vczh

https://www.zhihu.com/question/19918532/answer/58538334

 

分享到:更多 ()