diff --git a/README.Rmd b/README.Rmd index 3e2cec8..b479108 100644 --- a/README.Rmd +++ b/README.Rmd @@ -42,7 +42,7 @@ remotes::install_github("dereckmezquita/interface") Import the package functions. ```{r} -box::use(interface[ interface, type.frame, fun ]) +box::use(interface[ interface, type.frame, fun, enum ]) ``` Define an interface and implement it: @@ -268,6 +268,51 @@ try(rbind(df, data.frame( ))) ``` +### Enums + +Define enums for categorical variables; these are safe to use to protect a value from being modified to invalid options. The `enum` function creates a generator which is then used to create the enum object. This can be used standalone or as part of an interface. + +```{r enum} +Colour <- enum("red", "green", "blue") + +# Create an enum object +colour <- Colour("red") +print(colour) + +colour$value <- "green" +print(colour) + +# Invalid modification (throws error) +try(colour$value <- "yellow") + +# Use in an interface +Car <- interface( + make = enum("Toyota", "Ford", "Chevrolet"), + model = character, + colour = Colour +) + +# Implement the interface +car1 <- Car( + make = "Toyota", + model = "Corolla", + colour = "red" +) + +print(car1) + +# Invalid implementation (throws error) +try(Car( + make = "Honda", + model = "Civic", + colour = "yellow" +)) + +# Invalid modification (throws error) +try(car1$colour$value <- "yellow") +try(car1$make$value <- "Honda") +``` + ## Conclusion The `interface` package provides powerful tools for ensuring type safety and validation in R. By defining interfaces, typed functions, and typed data frames, you can create robust and reliable data structures and functions with strict type constraints. For more details, refer to the package documentation. diff --git a/README.md b/README.md index 0e6e5d9..af4569c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ remotes::install_github("dereckmezquita/interface") Import the package functions. ``` r -box::use(interface[ interface, type.frame, fun ]) +box::use(interface[ interface, type.frame, fun, enum ]) ``` Define an interface and implement it: @@ -121,7 +121,7 @@ print(john_student) #> scores: Science #> scores: 95 #> scores: 88 -#> scholarship: +#> scholarship: #> street: 123 Main St #> city: Small town #> postal_code: 12345 @@ -167,8 +167,8 @@ try(UserProfile( age = "30" )) #> Error : Errors occurred during interface creation: -#> - Invalid value(s) for property 'email' at index(es): 1 -#> - Invalid value(s) for property 'age' at index(es): 1 +#> - Invalid value for property 'email': FALSE +#> - Invalid value for property 'age': FALSE ``` ### Typed Functions @@ -357,6 +357,69 @@ try(rbind(df, data.frame( #> Row 1 failed validation: Age must be less than 40 (got 50) ``` +### Enums + +Define enums for categorical variables; these are safe to use to protect +a value from being modified to invalid options. The `enum` function +creates a generator which is then used to create the enum object. This +can be used standalone or as part of an interface. + +``` r +Colour <- enum("red", "green", "blue") + +# Create an enum object +colour <- Colour("red") +print(colour) +#> Enum: red + +colour$value <- "green" +print(colour) +#> Enum: green + +# Invalid modification (throws error) +try(colour$value <- "yellow") +#> Error in `$<-.enum`(`*tmp*`, value, value = "yellow") : +#> Invalid value. Must be one of: red, green, blue + +# Use in an interface +Car <- interface( + make = enum("Toyota", "Ford", "Chevrolet"), + model = character, + colour = Colour +) + +# Implement the interface +car1 <- Car( + make = "Toyota", + model = "Corolla", + colour = "red" +) + +print(car1) +#> Object implementing interface: +#> make: Toyota +#> model: Corolla +#> colour: red +#> Validation on access: Disabled + +# Invalid implementation (throws error) +try(Car( + make = "Honda", + model = "Civic", + colour = "yellow" +)) +#> Error in validator(value) : +#> Invalid value. Must be one of: Toyota, Ford, Chevrolet + +# Invalid modification (throws error) +try(car1$colour$value <- "yellow") +#> Error in `$<-.enum`(`*tmp*`, value, value = "yellow") : +#> Invalid value. Must be one of: red, green, blue +try(car1$make$value <- "Honda") +#> Error in `$<-.enum`(`*tmp*`, value, value = "Honda") : +#> Invalid value. Must be one of: Toyota, Ford, Chevrolet +``` + ## Conclusion The `interface` package provides powerful tools for ensuring type safety