Try not to write code like this in statically, nominally typed languages (Java, Kotlin, C++, etc.). Without a good compile-time metaprogramming, you're bound to need code like this sooner rather than later [EDIT: you still should try to minimize it; it shouldn't be the first solution you reach for, but it really is unavoidable in many cases]. It's a PITA, but that's just it is in those languages - and Copilot works pretty well as an ad hoc code-gen tool.