Horse := new_class(properties = list(
name = class_character,
colour = class_character,
height = new_property(class_numeric, default = 15),
age = new_property(
class_numeric,
validator = function(value) if (value < 0) "must be positive"
),
now = new_property(getter = function(self) Sys.time())
))
prop_names(Horse)
#> [1] "name" "parent" "package" "properties" "abstract"
#> [6] "constructor" "validator"
prop_exists(Horse, "col")
#> [1] FALSE
prop_exists(Horse, "colour")
#> [1] FALSE
prop_info(Horse)
#> name default class getter setter validator
#> 1 name <character> FALSE FALSE FALSE
#> 2 colour <character> FALSE FALSE FALSE
#> 3 height 15 <integer> or <double> FALSE FALSE FALSE
#> 4 age <integer> or <double> FALSE FALSE TRUE
#> 5 now <ANY> TRUE FALSE FALSE
# All functions also work with objects, not just classes
lex <- Horse(colour = "bay", height = 15, name = "Lex", age = 3)
prop_names(lex)
#> [1] "name" "colour" "height" "age" "now"
prop_exists(lex, "age")
#> [1] TRUE