This function declare a new class, and copies attributes and methods from parent classes.
Usage
new_class(
...,
env = new.env(parent = parent.frame()),
class_name = NULL,
empty_class = FALSE
)
Arguments
- ...
Environments. Parent class environments.
- env
Environment. The new class environment.
- class_name
Name of the new class.
- empty_class
Boolean. Whether to create an empty class. This should only be used when you don't want to inherited from BASE, or you want to define your own base object class. Will be ignored if
...
is not empty. If...
is empty andempty_class == FALSE
, BASE will be used as the parent class.
Details
Parents can be provided in ...
, where methods and attributes will be
overrided by the left classes because bandicoot
does not support dynamic
dispatch at the moment. However, this behaviour usually aligns with the method
resolution order defined by the C3 algorithm used in Python.
If ...
is empty and empty_class == FALSE
,
BASE will be used as the parent class.
Examples
MYCLASS <- new_class(class_name = "MYCLASS")
MYCLASS
#>
#> ── <MYCLASS class>
names(MYCLASS)
#> [1] "..mro.." "..bases.." "..str.." "..len.."
#> [5] "..class.." "..new.." "has_attr" "del_attr"
#> [9] "..repr.." "set_attr" "..type.." "get_attr"
#> [13] "..dir.." "..methods.." "..method_env.." "..instantiated.."
#> [17] "..init.." "..class_tree.." "instantiate"
# Inhert from BASE class
TEST <- new_class(BASE, class_name = "TEST")
TEST
#>
#> ── <TEST class>
names(TEST)
#> [1] "..mro.." "..bases.." "..str.." "..len.."
#> [5] "..class.." "..new.." "has_attr" "del_attr"
#> [9] "..repr.." "set_attr" "..type.." "get_attr"
#> [13] "..dir.." "..methods.." "..method_env.." "..instantiated.."
#> [17] "..init.." "..class_tree.." "instantiate"