假设,我type Radians = distinct float
和type Degrees = distinct float
这不是让我使用可用的花车所有操作,甚至是最基本的算术+
,-
,*
有没有什么办法来排序“继承”所有的人都和使用distinct
只为编译时检查?
有关完整示例,请查看nim手册上“区别类型”中的“建模货币”部分。
综上所述:
使用借用杂注
proc `*` (x: int, y: Dollar): Dollar {.borrow.}
proc `div` (x: Dollar, y: int): Dollar {.borrow.}
使用模板减少样板
template multiplicative(typ, base: typedesc) =
proc `*` *(x: typ, y: base): typ {.borrow.}
proc `*` *(x: base, y: typ): typ {.borrow.}
proc `div` *(x: typ, y: base): typ {.borrow.}
proc `mod` *(x: typ, y: base): typ {.borrow.}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句