The code below won't compile, but, if order of the init procs are changed - it would work, why?
func init*[R, A](_: type[seq[R]], list: seq[A]): seq[R] =
for v in list:
result.add R.init(v)
type OptionParams* = tuple
right: string
tenor: float
moneyness: float
func init*(_: type[OptionParams], v: OptionParams): OptionParams = v
echo (seq[OptionParams]).init(@[("call", 120.0, 1.2)])
If order changed, as in example below, it would work:
type OptionParams* = tuple
right: string
tenor: float
moneyness: float
func init*(_: type[OptionParams], v: OptionParams): OptionParams = v
func init*[R, A](_: type[seq[R]], list: seq[A]): seq[R] =
for v in list:
result.add R.init(v)
echo (seq[OptionParams]).init(@[("call", 120.0, 1.2)])
Because the proc that uses R.init(v) uses what is called a "closed symbol choice" for init, meaning only the init definitions before it are possible symbol choices. To prevent this, use the mixin statement like so:
func init*[R, A](_: type[seq[R]], list: seq[A]): seq[R] =
mixin init
for v in list:
result.add R.init(v)