new_union {S7} | R Documentation |
Define a class union
Description
A class union represents a list of possible classes. You can create it
with new_union(a, b, c)
or a | b | c
. Unions can be used in two
places:
To allow a property to be one of a set of classes,
new_property(class_integer | Range)
. The defaultdefault
value for the property will be the constructor of the first object in the union. This means if you want to create an "optional" property (i.e. one that can beNULL
or of a specified type), you'll need to write (e.g.)NULL | class_integer
.As a convenient short-hand to define methods for multiple classes.
method(foo, X | Y) <- f
is short-hand formethod(foo, X) <- f; method(foo, Y) <- foo
S7 includes built-in unions for "numeric" (integer and double vectors), "atomic" (logical, numeric, complex, character, and raw vectors) and "vector" (atomic vectors, lists, and expressions).
Usage
new_union(...)
Arguments
... |
The classes to include in the union. See |
Value
An S7 union, i.e. a list with class S7_union
.
Examples
logical_or_character <- new_union(class_logical, class_character)
logical_or_character
# or with shortcut syntax
logical_or_character <- class_logical | class_character
Foo <- new_class("Foo", properties = list(x = logical_or_character))
Foo(x = TRUE)
Foo(x = letters[1:5])
try(Foo(1:3))
bar <- new_generic("bar", "x")
# Use built-in union
method(bar, class_atomic) <- function(x) "Hi!"
bar
bar(TRUE)
bar(letters)
try(bar(NULL))