I have having trouble creating a generic type that can spawn other objects with a shared resource but different composite types.
type
Foo[A, B] = object
a: A
b: B
x: int
proc initFoo[A, B](x: int): Foo[A, B] =
result.x = x
proc deriveFoo[A, B](f: Foo[A, B]): Foo[A, B] =
result.x = f.x
proc deriveDifferentFoo[A, B](f: Foo): Foo[A, B] =
result.x = f.x
let f = initFoo[int, float](5) # works
let g = f.deriveFoo # works
let h = f.deriveDifferentFoo[string, float](g) # fails with error: Cannot Instantiate A
f.doThing[x] is interpreted as [](f.doThing, x) as such you need to use the f.doThing[:x]() syntax ala:
type
Foo[A, B] = object
a: A
b: B
x: int
proc initFoo[A, B](x: int): Foo[A, B] =
result.x = x
proc deriveFoo[A, B](f: Foo[A, B]): Foo[A, B] =
result.x = f.x
proc deriveDifferentFoo[A, B](f: Foo): Foo[A, B] =
result.x = f.x
let f = initFoo[int, float](5) # works
let g = f.deriveFoo # works
let h = f.deriveDifferentFoo[: string, float]()
Thank you very much!
Now considering if using typedesc params might be a smoother UI but maybe f.deriveDifferentFoo[: string, float]() is fine.