The variant of an R8 enum value, to branch on with switch() or to show.
A struct value has no variant.
Details
A switch() without a last, unnamed arm returns NULL for a name that it
does not list; an arm that stops reports it, as in the example.
See also
r8is_variant(), which tests for one variant.
Examples
op <- options(r8.namespace = "demo")
Shape <- new_enum(
"Shape",
variants = list(
Circle = variant(fields = list(r = type_double(min = 0))),
Square = variant(fields = list(side = type_double(min = 0)))
)
)
area <- function(x) {
switch(
r8variant(x),
Circle = pi * x$r^2,
Square = x$side^2,
stop("unsupported shape: ", r8variant(x))
)
}
area(Shape@Square(side = 2))#> [1] 4#> [1] TRUEoptions(op)