Skip to contents

Create a new enum class

Usage

new_enum(
  name,
  variants,
  package = topenv_package_name(parent.frame()),
  allow_na = TRUE
)

Arguments

name

String. Name of the new class.

variants

Character vector. Allowed values.

package

String or NULL. Package to attribute the class to. Defaults to the caller's package, if any.

allow_na

Bool. Whether NA_character_ is a valid value.

Value

An S7 class generator that inherits from Enum.

Examples

GridShape <- new_enum("GridShape", c("Square", "Hexagon"))
#> Overwriting method convert(<character>, <GridShape>)
GridShape("Square")
#> <GridShape>
#>  @ value   : chr "Square"
#>  @ variants: chr [1:2] "Square" "Hexagon"
#>  @ allow_na: logi TRUE
GridShape(NA_character_)
#> <GridShape>
#>  @ value   : chr NA
#>  @ variants: chr [1:2] "Square" "Hexagon"
#>  @ allow_na: logi TRUE