Skip to content

Replace switch dispatch with dynamic function calls in create_default_parameters.R#1237

Draft
Copilot wants to merge 3 commits intodevfrom
copilot/simplify-create-default-selectivity
Draft

Replace switch dispatch with dynamic function calls in create_default_parameters.R#1237
Copilot wants to merge 3 commits intodevfrom
copilot/simplify-create-default-selectivity

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

Eliminates manual switch statement updates when adding new selectivity, maturity, recruitment, or distribution modules.

Changes

Replaced explicit switch() statements with dynamic dispatch using do.call(get()):

Before:

default <- switch(form,
  "Logistic" = create_default_Logistic(),
  "DoubleLogistic" = create_default_DoubleLogistic()
) |>
  dplyr::mutate(module_name = "Selectivity")

After:

default <- do.call(get(paste("create", "default", form, sep = "_")), list()) |>
  dplyr::mutate(module_name = "Selectivity")

Affected Functions

  • create_default_selectivity() - dispatches to create_default_<form>()
  • create_default_maturity() - dispatches to create_default_<form>()
  • create_default_recruitment() - dispatches to create_default_<form>Recruitment(data)
  • Distribution helpers (3x) - dispatch to create_default_<distribution>Distribution(...)

All inputs are validated by rlang::arg_match() or explicit checks before dispatch. Function naming conventions remain unchanged: create_default_* for modules, create_default_*Recruitment for recruitment forms, create_default_*Distribution for distributions.

Net: -20 lines

Original prompt

This section details on the original issue you should resolve

<issue_title>[Developer Issue]: Simplify create_default_selectivity()</issue_title>
<issue_description>### Description

When you add new selectivity modules you also have to update the create_default_selectivity() function in create_default_parameters.R. There are other places where we have code similar to what is show below as well, e.g., maturity.

default <- switch(form,
"Logistic" = create_default_Logistic(),
"DoubleLogistic" = create_default_DoubleLogistic()
) |>

I suggest that we change the code above to the following so that we do not have to make additions each time we add a new module. What do you think @Bai-Li-NOAA?

default <- eval(call(paste("create", "default", form, sep = "_")))
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@Bai-Li-NOAA</author><body>
I think the original idea behind `switch` was that some R functions for the R modules might require different arguments, but I haven't found any existing example in the codebase. So yes, let's change it to `eval()`. 

Regarding who and when to change it, maybe have someone to do a global search on `switch()` and change all of them in a different PR?</body></comment_new>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 24, 2026 03:13
…/call pattern

Co-authored-by: kellijohnson-NOAA <4108564+kellijohnson-NOAA@users.noreply.github.com>
Co-authored-by: kellijohnson-NOAA <4108564+kellijohnson-NOAA@users.noreply.github.com>
Copilot AI changed the title [WIP] Simplify create_default_selectivity function Replace switch dispatch with dynamic function calls in create_default_parameters.R Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants