enum() creates a new enumeration, and infers the the enumeration type based on the arguments supplied to it. Enumerations are similar to lists, but:

  • cannot be assigned to after definition

  • all values and names must be unique

There are two types of enumeration: generic and numeric. The main difference between the two is that generic enumerations must have all their values explicitly defined. Numeric enums, by contrast, infer the values of their members, and must have only numeric values.

Numeric enum values are inferred when a member does not have a value assigned to it. The value of the member is then calculated as +1 to the last member's value, or the current member's index. Incrementation for values is prioritised over index as value.

enum(...)

Arguments

...

list of enumeration arguments

Value

An enumeration (enum), a list of unique name/value pairs.

Details

Under the surface, enums are actually lists contained within locked environments. This is so that the enum bindings cannot be modified, and the enum order is maintained. S3 methods are defined for $, [, and [[, which access the enum list directly.

See also

Examples

# Generic Enum enum(apple = "apple", pear = "pear")
#> # A generic enum: 2 members #> chr apple : apple #> chr pear : pear
enum(dat1 = mtcars, dat2 = iris, dat3 = PlantGrowth)
#> # A generic enum: 3 members #> df dat1 : <32 × 11> #> df dat2 : <150 × 5> #> df dat3 : <30 × 2>
# Numeric Enum enum(style, warning, error)
#> # A numeric enum: 3 members #> num style : 1 #> num warning : 2 #> num error : 3
enum(a = 50, b = .$a * 2)
#> # A numeric enum: 2 members #> num a : 50 #> num b : 100