How do I get the expected return type of a polymorphic function at compile time?

You have run into one of the trickiest problems in using Julia performantly with generic functions and pre-allocated buffers.

Unfortunately, there is no generic solution. The cleanest way is probably defining an auxiliary function g_elt that works on types. Sometimes you can assemble it from some functions that work on types, eg see this and similar discussions.