Skip to content

Instantly share code, notes, and snippets.

@hadley
Created June 1, 2019 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/2a80f008e55527c6db3d696170c455fe to your computer and use it in GitHub Desktop.
Save hadley/2a80f008e55527c6db3d696170c455fe to your computer and use it in GitHub Desktop.
library(rlang)
enum_value <- function(x, values) {
structure(
x,
values = values,
)
}
enum <- function(...) {
val_sym <- ensyms(...)
val_str <- vapply(val_sym, x, as.character, character(1))
val <- lapply(val_str, enum_value, values)
structure(
val,
class = "enum"
)
}
`[[.enum`` <- function(x, i) {
if (i %in% names(x)) {
stop("Invalid")
}
NextMethod()
}
`$.enum` <- function(x, i) {
if (i %in% names(x)) {
stop("Invalid")
}
NextMethod()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment