Are the types known at compile-time or do they depend on runtime data.
Otherwise you can do compile-time dispatch:
type
Something[T] = object
a: int
special: T
SpecialisedTA = object
c: int
SpecialisedA = Something[SpecialisedTA]
SpecialisedTB = object
d: int
SpecialisedB = Something[SpecialisedTB]
Smth = Something[SpecialisedTA] or Something[SpecialisedTB]
proc doIt(strategy: static[int]): Smth =
when strategy == 0:
result = Something[SpecialisedTA](a: 0, special: SpecialisedTA(c: 30))
else:
result = Something[SpecialisedTB](a: 0, special: SpecialisedTB(d: 30))
let myThing = doIt(0)
echo myThing
Run
On a side note, it seems like you are trying to use the strategy pattern and
dispatch according to an integer. You should use an enum and case statement
instead or an object variant.
In most cases all "patterns" that might be used in OO languages can be replaced
by idiomatic Nim code that is in my opinion, more succinct, maintainable and
readable.