erlang的强数据类型

在mailist中,一位朋友表示疑问,为什么下面的语句提示出错?erlang的运行时数据绑定有什么特殊规则?
>{ok, File} = file:open(”test.file”, [write, raw, {delayed_write, math:pow(10, 3), 1000)]).
** exception error: no match of right hand side value {error,badarg}
提示badarg,参数错误。
其实不是运行时绑定有什么问题,而是math:pow/2返回的数据类型为float, 而file:open/2中的tuple的第二个element需要类型为integer,因此就出现了badarg错误。
修正办法:可以通过trunc/或者round/1将float转化为integer(trunc是将小数部分舍弃,而round是四舍五入,具体参看erlang模块), 当然更聪明的办法就是去掉math:pow/2这样的测试代码。
顺便说一下上例中file:open/2的{delayed_write, Size, Delay}的涵义,这是指要求将数据延迟写入文件,只有当数据达到Size字节,或者数据已经存活了Delay毫秒。具体参看file module.
在erlang中是没有c++中类似的隐式类型转换,这样做的目的是为了使语言简单,便于调试开发。
erlang是如此透彻,坚定。你只要掌握有限的几种数据类型,掌握功能强大的模式匹配,明白变量的一次数值绑定,勤写代码,勤看文档。
不要多久,你也可以写出简单,高校,易读的erlang应用。

boost - 智能指针之scoped_ptr

scoped_ptr不具备“value”语义,不可以进行拷贝和赋值,因此不涉及所有权(ownership)的转化,因此比起std::auto_ptr和boost:shared_ptr更加安全。
其只是遵循RAII(Resource Acquisition Is Initialisation)原则,对象初始化时即获取资源,对象析构时释放资源。
使用scoped_ptr可以简化代码,保证异常安全.
scoped_ptr实现简单,与原始指针效率相当.
注意:scoped_ptr不可以用在STL标准容器中;scoped_ptr不可以保存动态分配的数组指针
Example

#include <boost/scoped_ptr.hpp>
#include <iostream>

struct Shoe { ~Shoe() { std::cout << “discard my shoe\n”; } };

class MyClass {
boost::scoped_ptr<int> ptr;
public:
MyClass() : ptr(new int) { *ptr = 0; }
int add_one() { return ++*ptr; }
};

int main()
{
boost::scoped_ptr<Shoe> x(new Shoe);
[...]