我试图在 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]
, 每次编译,但我当然希望能够切换更复杂的生成器。
谁能帮我理解编译器错误?