Never done metaprogramming and now I am thinking I have the perfect use case for i

Never done metaprogramming and now I am thinking I have the perfect use case for it. I have code that looks like the following and it’s quite tedious:

    if params.scr_start > 0 
        cbset = CallbackSet(cb_screen)
    end
    if params.npi_start > 0 
        cbset = CallbackSet(cb_npi) 
    end 
    if params.scr_start > 0 && params.npi_start > 0 
        cbset = CallbackSet(cb_npi, cb_screen)
    end
    # ... some other combinations 

is there a way that a macro would help me out? Something like @cbset params.scr_start params.npi_start which can then generate the proper variable for me?

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

Any drawback to using something like this?

cbset = CallbackSet([cb_scr, cb_npi][[params.scr_start, params.npi_start] .> 0]...)

Or similar using filter().

1 Like