语言漫谈:Primitive, or non-primitive, that is the question
来源:互联网 发布:爱美眉美女图片站源码 编辑:程序博客网 时间:2024/06/03 16:34
语言漫谈:Primitive, or non-primitive, that is the question
by JsWatcher, at csdn.net, 2013-0328
大概是2005年,Joshua Bloch, Neal Gafter 写了一本漫谈 Java 语言的书 《Java Puzzlers: Traps, Pitfalls, and Corner Cases》
里面提到一个 float 运算的问题,2.00 - 1.10, 会得出 0.89999999, 而不是 0.90。
由这个问题,作者提出了,应该把 java.math.BigDecimal 类 primitive 化。原文是:
For language designers, consider providing linguistic support for decimal arithmetic. One approach is to offer limited support for operator overloading, so that arithmetic operators can be made to work with numerical reference types, such as BigDecimal. Another approach is to provide a primitive decimal type, as did COBOL and PL/I.
于是这里有一个基本问题:
程序语言中,对于一种数据型 data type, 要不要提供 primitive type (原始类型,原语类型)的支持?
引用伟大的莎士比亚的名句:To be, or not to be, that is the question.
or simply,
Primitive, or non-primitive, that is the question。
这里,与大家聊聊我的看法。
这相当程度上取决于,是否有 hardware 支持,从而取得 best performance.
因为 primitive type 多都是值运算,总是 copy, 所以 performance 很重要!
典型的例子是 int, 它在各种语言中都有很好的支持,因为,它是 natively supported by hardware. 它就是 cpu 的 register. float 也是,它有专门的 hardware unit 支持它的运算。
String 是反例,在 java 它是 “准primitive” ,language supported simple type. 在 C/C++ 里它不是。因为,它没有hardware 支持。
对于那些没有 hardware 支持的 data type, 是否要创建 primitive type, 就取决于应用的方便,是否对开发者有利。而这就是一个萝卜白菜各有所好的事情。常回争论不休,各抒己见。
注意,一旦成为 primitive type, 就会损失扩张性,除非是像 smalltalk 那种语言。
像 String,java 语言的设计者,就把它 “准primitive” 觉得有用,好处大。另一边,C/C++语言的设计者,就不这样想,有 char 和 pointer 足够好用。
对于 BigDecimal 就是这类没有 hardware 直接支持的 data type, 所以众人各抒己见,各有各的高论。
我个人的观点是,没有 hardware 直接支持,就没有必要 primitive 化。BigDecimal 就属于此类。有好的 package class 支持就行。这样不失灵活性,不满意别人的,就定义自己的。
拿 java 的 String 作例子,用它时很少会意识到它的 “准primitive” language supported 地位,想扩张时,一拍脑门,才意识到,我的妈,它是 language supported, simple type, 行为很难改。
注:这篇文章来源于,我在论坛的讨论,感谢 dracularking 提出的话题。原贴见:http://bbs.csdn.net/topics/390405984
- 语言漫谈:Primitive, or non-primitive, that is the question
- To be or Not to be that is the question
- Solr or Elasticsearch–That Is the Question
- GYM 101061 D.Max or Min .. that is the question!(水~)
- to be or not to be, that is a question...
- To be or not to be that is a question
- Issue 111( Most important discoveries or creation are accidental; it is usually while seeking the answer to one question that
- To be, or not to be; that is the question! 生存还是毁灭,这是个值得思考的问题。
- the Primitive Built-in Types bool
- Primitive roots
- 渲染primitive
- Primitive root
- primitive calculator
- Java Primitive
- Primitive Topology
- copy wrapper tpye value to primitive type in simlar obejct except its fileds is primitive type
- 当IE向服务器发送数据的时候,这些数据是IE从哪里取得的呢?To block, or not to block? That is the question.
- 发现运行时会报错误, Object is not a primitive:
- 人工智能界(AI)‘牛牛’主页&坐镇机器学习(machine learning)的江湖大家
- mysql存储IP地址省空间的方法
- FSMD
- mysql性能优化-慢查询分析、优化索引和配置
- C语言解释器-15 语法分析之辅助工具
- 语言漫谈:Primitive, or non-primitive, that is the question
- JavaScript setInterval方法IE下不支持传递参数
- DB2中列的唯一值的定义
- 如何导入outlook联系人到BlackBerry 10
- 错误位置的头文件声明导致添加类变量出错
- viso图片转eps文件 AND pdf转eps文件
- iwork09批量授权码
- Node.js Express
- Hibernate 面试中最常考察的知识点整合