knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(enumr, quietly = TRUE)

You can use . (called "dot") to refer to the current enum. Due to this, when defining variables, you can use .$ to refer to other elements in the enum. E.g.

# Numeric enum
enum(
    a,
    b,
    c = .$a + 10,
    d = .$c * .$b
)

enum(
    a = "Hello",
    b = c(.$a, "world")
)

This means you can do some neat stuff with enum definition. For example:

enum(
    base = "www.test.com/%s",
    download = sprintf(
        .$base,
        "download"
    ),
    upload = sprintf(
        .$base,
        "upload"
    )
)

Limitations

It should be noted, however, there are limitations to defining members with .$. Firstly, enum member validation is performed sequentially. That is, each non-explicit member is initialised in order of their definition. We can see how this plays out "under the hood" by using the dot operator:

enum(
    a = .,
    b = .,
    c = .
)

As above, the enum structure changes with each member's definition (remember that the dot operator refers to the current enum).

As a result, computed members cannot refer to later non-explicit members. For instance:

# Invalid definition!
enum(
    a,
    b = .$c + 1,
    c
)

# Valid due to explicit initialisation
enum(
    a,
    b = .$c + 1,
    c = 50
)

enum(
    a = .$c + 1,
    b = .$a + 1,
    c = 5
)

Moreover, due to sequential initialisation (and the pains of recursion), enum members do not support cyclical evaluation.

# Invalid
enum(
    a = .$b + 1,
    b = .$c + 1,
    c = .$a + 1
)


ElianHugh/enumr documentation built on Dec. 17, 2021, 6:25 p.m.