# "ANY" takes a value of any type;
# validity keeps both sides one type
setClass("Size",
slots = c(width = "ANY", height = "ANY"),
validity = function(object) {
w_class <- class(object@width)
h_class <- class(object@height)
if (identical(w_class, h_class)) TRUE
else "width and height differ in type"
})
as_text <- function(size) {
paste0("[", size@width, "; ", size@height, "]")
}
size_int <- new("Size", width = 5L, height = 8L)
text_int <- as_text(size_int)
# text_int = [5; 8]
size_float <- new("Size", width = 3.7,
height = 1.58)
text_float <- as_text(size_float)
# text_float = [3.7; 1.58]
size_str <- new("Size", width = "12dp",
height = "7dp")
text_str <- as_text(size_str)
# text_str = [12dp; 7dp]
cat("text_int =", text_int, "\n")
cat("text_float =", text_float, "\n")
cat("text_str =", text_str, "\n")