关于语言类型系统

查看知乎


http://www.zhihu.com/question/19918532


个人比较认同的答案:

著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处。
作者:Kaiye Chang
链接:http://www.zhihu.com/question/19918532/answer/23217475
来源:知乎

长了点,希望能耐心看完。看过不同的文章后,整理出来的

中文解释(底下有英文解释):

==============================

动态语言vs静态语言(动态类型语言vs静态类型语言)
Static typing when possible, dynamic typing when needed

型态系统(type system):程序中专门处理数据的系统。语言可以分为
动态型态系统(dynamically typed systems),例如Lisp,JavaScript,Tcl和Prolog
静态型态系统(statically typed systems),例如C++和Java

静态型态系统可进一步分为
包含宣告型态(manifest type)语言,即每一个变量和函数的型态都清楚地宣告
type-inferred语言(例如MUMPS,ML)

==============================

动态类型语言(Dynamically Typed Language):

运行期间才做数据类型检查的语言,即动态类型语言编程时,永远不用给任何变量指定数据类型。该语言会在第一次赋值给变量时,在内部将数据类型记录下来。
例如:ECMAScript(JavaScript)、Ruby、Python、VBScript、php
Python和Ruby就是典型动态类型语言,其他各种脚本语言如VBScript也多少属于动态类型语言
优点:方便阅读,不需要写非常多的类型相关的代码;
缺点:不方便调试,命名不规范时会造成读不懂,不利于理解等

静态类型语言(Statically Typed Language):

编译期间做检查数据类型的语言,即写程序时要声明所有变量的数据类型,是固定的。使用数据之前,必须先声明数据类型(int ,float,double等)。相当于使用之前,首先要为它们分配好内存空间。
例如:C/C++是静态类型语言的典型代表,其他的静态类型语言还有C#、JAVA等
优点:结构非常规范,便于调试,方便类型安全
缺点:为此需要写更多类型相关代码,不便于阅读、不清晰明了

强类型定义语言(Explicit type conversion,强制数据类型定义语言,类型安全的语言):

一旦变量被指定某个数据类型,如果不经强制转换,即永远是此数据类型。

举例:若定义了一个整型变量a,若不进行显示转换,不能将a当作字符串类型处理
强类型语言是指需要进行变量/对象类型声明的语言,一般情况下需要编译执行。例如C/C++/Java/C#

弱类型定义语言(Implicit type conversion,类型不安全的语言):

数据类型可以被忽略的语言。它与强类型定义语言相反, 一个变量可以赋不同数据类型的值。

举例:在VBScript中,可以将字符串 '12' 和整数 3 进行连接得到字符串 '123', 然后可以把它看成整数 123,而不需要显示转换
例如PHP/ASP/Ruby/Python/Perl/ABAP/SQL/JavaScript/Unix Shell等

注意:强类型定义语言在速度上可能略逊色于弱类型定义语言,但是强类型定义语言带来的严谨性能够有效的避免许多错误。

==============================

“语言是否动态”与“语言是否类型安全”之间是完全没有联系的!

Python是动态语言,是强类型定义语言(类型安全的语言);
VBScript是动态语言,是弱类型定义语言(类型不安全的语言);
JAVA是静态语言,是强类型定义语言(类型安全的语言)

==============================

Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.

==============================

英文解释 (from 《Dive Into Python》)
2.2. Declaring Functions

Statically typed language
A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.
Dynamically typed language
A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
Explicit typed language
A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can't treat it like a string without explicitly converting it.
Implicit typed language
A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.

关于Python的类型:
Python is both dynamically typed (because it doesn't use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值