Learn R Programming

interface (version 0.1.2)

enum: Create an enumerated type

Description

Creates an enumerated type with a fixed set of possible values. This function returns an enum generator, which can be used to create enum objects with values restricted to the specified set.

Usage

enum(...)

Value

A function (enum generator) of class 'enum_generator' that creates enum objects of the defined type. The returned function takes a single argument and returns an object of class 'enum'.

Arguments

...

The possible values for the enumerated type. These should be unique character strings.

See Also

interface for using enums in interfaces

Examples

Run this code
# Create an enum type for colors
Colors <- enum("red", "green", "blue")

# Create enum objects
my_color <- Colors("red")
print(my_color)  # Output: Enum: red

# Trying to create an enum with an invalid value will raise an error
try(Colors("yellow"))

# Enums can be used in interfaces
ColoredShape <- interface(
  shape = character,
  color = Colors
)

my_shape <- ColoredShape(shape = "circle", color = "red")

# Modifying enum values
my_shape$color$value <- "blue"  # This is valid
try(my_shape$color$value <- "yellow")  # This will raise an error

Run the code above in your browser using DataLab