数据类型是抽象的敌人吗?

Are datatypes the enemy of abstraction?

Software Abstractions 一书的第 137 页有这些非凡的陈述:

Integers are actually not very useful. If you think you need them, think again; there is often a more abstract description that matches the properties better. Just because integers appear in the problem domain does not mean that they should be modeled as such. To figure out whether integers are necessary, ask yourself what properties are actually relied upon. For example, a communication protocol that numbers its messages may rely only on the numbers being distinct; or it may rely on them increasing; or perhaps even being totally ordered. In none of these cases should integers be used.

哇!

这很重要。我想深入了解一下。

为了帮助我理解,我创建了两个版本的通信协议。

第一个版本使用 Int 数据类型:

sig Message {
    number: Int
}

第二个版本没有:

sig Message {
    number: Number 
}    
sig Number {}

第二个版本是不是比较抽象?怎么比较抽象?它更抽象是因为 Number 不依赖于数据类型吗?第一个版本不那么抽象,因为它指定了一个数据类型(Int)?

数据类型是抽象的敌人吗?

不,第二个也好不到哪去。假设您的消息不是完全有序的,而是部分有序的。那么关键是,与其为每条消息分配一个索引,不如明确地对消息进行部分排序:

sig Message {follows: set Message, ...}