Resampler <- new_enum(
"Resampler",
description = "How cases are split into training and test sets.",
variants = list(
KFold = variant(
description = "K-fold cross-validation.",
fields = list(
n_resamples = field(
type_integer(min = 2L),
default = 10L,
description = "Number of folds."
),
seed = option(type_integer(min = 0L))
)
),
StratSub = variant(
description = "Stratified subsampling.",
fields = list(
n_resamples = field(type_integer(min = 1L), default = 10L),
train_p = field(
type_double(exclusive_min = 0, exclusive_max = 1),
default = 0.75,
description = "Fraction of cases in each training set."
),
seed = option(type_integer(min = 0L))
)
),
Bootstrap = variant(
description = "Bootstrap resampling: cases drawn with replacement.",
fields = list(
n_resamples = field(type_integer(min = 1L), default = 10L),
seed = option(type_integer(min = 0L))
)
)
)
)4 Enums
An enum is a type whose values are each exactly one of its variants. new_enum() declares one, and variant() a variant with its fields, declared as a struct’s are. Resampler has a variant for each way to split cases into training and test sets:
4.1 Values
@ gives the constructor of a variant:
cv <- Resampler@KFold(seed = 2026L)
cv<demo::Resampler@KFold> n_resamples: 10 seed: 2026
A variant without fields is a unit variant, and @ gives its value:
Loss <- new_enum("Loss", variants = list(Squared = variant(), Absolute = variant()))
Loss@Squared<demo::Loss@Squared>
4.2 Variants
r8variant() names a value’s variant, and r8is_variant() tests for one:
r8variant(cv)[1] "KFold"
r8is_variant(cv, "KFold")[1] TRUE
r8variant(cv) %in% c("KFold", "StratSub")[1] TRUE
A name that the enum does not declare is an error, so a misspelled one is found where it is written:
r8is_variant(cv, "KFol")Error:
! `KFol` is not a variant of `demo::Resampler`; the nearest variant is `KFold`. Variants: KFold, StratSub, Bootstrap.
Code branches on the variant with switch(). A last arm that stops reports a variant that the others do not list, for which switch() returns NULL:
switch(
r8variant(cv),
KFold = sprintf("%d folds", cv$n_resamples),
StratSub = sprintf("%d subsamples", cv$n_resamples),
Bootstrap = sprintf("%d bootstraps", cv$n_resamples),
stop("unsupported resampler: ", r8variant(cv))
)[1] "10 folds"
Generics and methods writes such a branch in a method.
A value has the fields of its own variant only:
cv$train_pError:
! `train_p` is not a field of `demo::Resampler@KFold`. Fields: n_resamples, seed.
4.3 Validation
A variant’s constructor checks its fields as a struct’s does:
Resampler@StratSub(train_p = 1.2)Error:
! Invalid `demo::Resampler@StratSub`: 1 problem
- `train_p` must be below 1; found 1.2. [above_max]
4.4 Enums as fields
A field may hold an enum, with one of its values as the default:
Tuning <- new_struct(
"Tuning",
fields = list(
max_evals = field(type_integer(min = 1L), default = 100L),
resampler = field(Resampler, default = Resampler@KFold(n_resamples = 5L))
)
)
Tuning(max_evals = 50L)<demo::Tuning> max_evals: 50 resampler: <demo::Resampler@KFold> n_resamples: 5 seed: NULL