类型相等与类型区分
以下算法(伪代码)确定两种类型是否相等而不是 不同 类型。 为简洁起见,省略了辅助集 s 的循环检查:
- proc typeEqualsOrDistinct(a, b: PType): bool =
- if a.kind == b.kind:
- case a.kind
- of int, intXX, float, floatXX, char, string, cstring, pointer,
- bool, nil, void:
- # leaf type: kinds identical; nothing more to check
- result = true
- of ref, ptr, var, set, seq, openarray:
- result = typeEqualsOrDistinct(a.baseType, b.baseType)
- of range:
- result = typeEqualsOrDistinct(a.baseType, b.baseType) and
- (a.rangeA == b.rangeA) and (a.rangeB == b.rangeB)
- of array:
- result = typeEqualsOrDistinct(a.baseType, b.baseType) and
- typeEqualsOrDistinct(a.indexType, b.indexType)
- of tuple:
- if a.tupleLen == b.tupleLen:
- for i in 0..a.tupleLen-1:
- if not typeEqualsOrDistinct(a[i], b[i]): return false
- result = true
- of distinct:
- result = typeEqualsOrDistinct(a.baseType, b.baseType)
- of object, enum:
- result = a == b
- of proc:
- result = typeEqualsOrDistinct(a.parameterTuple, b.parameterTuple) and
- typeEqualsOrDistinct(a.resultType, b.resultType) and
- a.callingConvention == b.callingConvention
- elif a.kind == distinct:
- result = typeEqualsOrDistinct(a.baseType, b)
- elif b.kind == distinct:
- result = typeEqualsOrDistinct(a, b.baseType)