Skip to content

Add gradethis_default() lookup helper #245

Open

Description

@gadenbuie

Currently, we lookup gradethis options via getOption("gradethis.fail", "Fall back default"), which has a few disadvantages:

  • the correct fallback needs to be used in every case
  • it clutters the API since the preferred method for setting the defaults is via gradethis_setup()
  • it isn't easy to differentiate between actively supplied arguments and default values determined from global settings without resorting to sentinel values that further obscure the mechanics

We should add a helper for looking up global default values: gradethis_default()

fail <- function(
  message = gradethis_default("fail"),
  ...,
  hint = gradethis_default("fail.hint"),
  encourage = gradethis_default("fail.encourage")
) { ... }

where the documentation can better direct users to gradethis_setup() where those values are set since both functions would be documented in the same help page.

Internally, gradethis_default() could add an attr() that we could use to determine whether or not the argument value was supplied by the user or looked up from global options.

Activity

  1. gadenbuie commented on Sep 21, 2021

    @gadenbuie
    MemberAuthor

    I implemented a version of this that — rather than taking the option as a character argument as in gradethis_default("<option>") — is instead a list of functions that access the current default value. The list is named gradethis_settings and its structure enables auto-complete:

    image

    I was thinking about a developer-facing interface at the time, hence gradethis_settings rather than gradethis_default to avoid clashes with our internal list of default values.

    With a little extra work, we could allow the functions to also operate as setters when provided with a value.

    # Set fail.hint default value
    gradethis_settings$fail.hint(TRUE)

    If we export this object, then the function signature for fail() (and others) would look like this:

    fail <- function(
      message = gradethis_settings$fail(),
      ...,
      hint = gradethis_settings$fail.hint(),
      encourage = gradethis_settings$fail.encourage()
    ) { 
      # ... 
    }

    I'm not completely sold on the name, gradethis_config or gradethis_options also come to mind, but I went with gradethis_settings since it's closer to gradethis_setup().

  2. self-assigned this
    on Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions