This sort of thing was done in the C and Python backends with templates.