knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(typed, quietly = TRUE) library(enumr, quietly = TRUE)
If you are using {typed}, you can make use of the Enum
assertion for return and input assertion. Note that enum()
is the class definition and Enum()
is the type assertion - this matches the {typed} paradigm of assertions (e.g. list()
vs typed::List()
).
Depending on how you define a variable, you can constrain a variable to either an enum class or enum member. To constrain to an enum class, simply use Enum()
. To constrain to an enum member, use Enum(x)
, with x being an enum.
For instance, let's say we want to ensure that the arguments supplied to a function correspond to an enum member value. Let's create an enum with a list of animals:
my_pets <- enum( pet1 = "dog", pet2 = "cat", pet3 = "goldfish" )
Now, I want to make a function that only allows the my_pets enum as an argument:
# supply my_pets in the Enum assertion is_this_my_pet <- ? function(arg = ? Enum(my_pets)) { print(arg) } # supply an incorrect argument is_this_my_pet("snake") # correct arg is_this_my_pet("dog")
See {typed} for more information on how to use these assertions.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.