【发布时间】:2015-01-14 17:55:06
【问题描述】:
我正在尝试在 Nim 中简单地使用类型类。请记住,我从今天早上开始才使用 Nim,所以我可能一直在做一些愚蠢的事情。
无论如何,我想定义一个伪随机生成器,它产生T 类型的值流。有时T 是数字,因此了解可达到的最小值和最大值是有意义的——比如重新调整值。这是我的类型
type
Generator*[T] = generic x
next(var x) is T
BoundedGenerator*[T] = generic x
x is Generator[T]
min(x) is T
max(x) is T
我也有这样的例子,比如LinearCongruentialGenerator。
假设我想用它来定义Uniform 生成器,它在一个区间内产生浮点值。我试过了
type Uniform* = object
gen: BoundedGenerator[int]
min_p: float
max_p: float
proc create*(gen: BoundedGenerator[int], min: float, max: float): Uniform =
return Uniform(gen: gen, min_p: min, max_p: max)
我省略了next、min 和max 的明显定义。
但是,由于Error: 'BoundedGenerator' is not a concrete type,上述内容无法编译
如果我明确地将LinearCongruentialGenerator 替换为BoundedGenerator[int],一切都会编译,但我当然希望能够切换更复杂的生成器。
谁能帮我理解编译器错误?
【问题讨论】: