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:

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 as_class() for details.

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))

[Package S7 version 0.1.1 Index]