If blocks with code generation

I agree with @lmiq. Metaprogramming is super useful when it’s needed, but can also make your code harder to maintain, and should be reserved for cases where it’s really needed. Here, you can get a lot done with multiple dispatch on helper functions. For eg.

You can have a get_other_values function, with get_other_values(t::B) returning a named tuple of the other values, and get_other_values(t::A) returning nothing. Then within loss,

you can have a do_things_with_other_values call, with one method of it accepting named tuples and computing things with its values, another method being do_things_with_other_values(::Nothing) = return .

These are just examples to illustrate the idea, a lot depends on the specifics of your code. Exploiting the type system and dispatch can give you performant, generic code, while being much more readable and maintainable than a metaprogramming approach.

2 Likes