Skip to content

.default() method for the grid_*() functions #429

Closed

Description

@hfrick

We have .default() methods for several other generics, but not for the grid functions. (The one for encode_unit() needs fixing: #430.)

library(dials)
#> Loading required package: scales

# no default method
grid_regular("foo")
#> Error in `UseMethod()`:
#> ! no applicable method for 'grid_regular' applied to an object of class "character"

# with default method
parameters("foo") 
#> Error in `parameters()`:
#> ! <parameters> objects cannot be created from a string.

finalize("foo")
#> Error in `finalize()`:
#> ! Cannot finalize a string.

encode_unit("foo")
#> Error in `encode_unit()`:
#> ! argument "direction" is missing, with no default

Created on 2026-02-23 with reprex v2.1.1

Activity

  1. hfrick commented on Apr 7, 2026

    @hfrick
    MemberAuthor

    Fix

    We might change what happens if x is missing (see #428) but until then, we need to catch that case. Otherwise {.obj_type_friendly {x}} fails.

    #' @export
    #' @rdname grid_regular
    grid_regular.default <- function(
      x,
      ...,
      levels = 3,
      original = TRUE,
      filter = NULL
    ) {
      if (missing(x)) {
        cli::cli_abort("At least one parameter object is required.")
      }
      cli::cli_abort(
        "{.arg x} must be a {.cls param} object, list, or {.cls parameters}, not
        {.obj_type_friendly {x}}."
      )
    }
    
  2. hfrick commented on Apr 7, 2026

    @hfrick
    MemberAuthor

    Add

      # wrong type
      expect_snapshot(error = TRUE, grid_random("not a param"))
    

    to test_that("grid_random() errors with non-param inputs", {...})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions