> runtime dispatch uses dispatch trees which are more performant than the more 
> common indirect branches

Don't quite understand your decision: the types seem to be known at compile 
time and nothing is faster than compile time dispatch. So, the solution by 
@mratsim should be fine. Btw., if you want a less restrictive type class to be 
returned by _doIt_ , try a concept: 
    
    
    type
      Something[T] = object
        a: int
        special: T
      
      SpecialisedTA = object
        c: int
      
      SpecialisedTB = object
        d: int
      
      Smth = concept s
        $s is string
        # plus this if needed later:
        # s.special is (SpecialisedTA or 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(1)
    
    echo myThing
    
    
    Run

Reply via email to