new_union | R Documentation |
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 default default
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 be NULL
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 for
method(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).
new_union(...)
... |
The classes to include in the union. See |
An S7 union, i.e. a list with class S7_union
.
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.