Well, I did try @jyapayne proposal, because the less code the less bugs, but it did not work in my case. The error message Error: in expression 'final_sort2(b)(ite``gensym19335006)'... occurs at the call site of final_sort2 and not the exception. It's perhaps related to the fact that the returned typedesc is being used to do a conversion by the compiler.
In the end, I reverted back to returning a typedesc to please the compiler, and then the exception occurs. For the moment, I'm using Nim type system to check acceptable types in the DSL, creating templates for the final_sort2 function: Nim type --> resulting DSL type. In the end, as the number of types will increase, I'll need a more flexible type checking system and I'll have to build a real table...