pub struct Class { /* private fields */ }
Expand description
Represents a class in the language.
§Instantiation
Using ()
operator after the class name will create a new instance of the class. The arguments
passed to the operator will be passed to the init
method of the class. The init
method is
optional and can be omitted. If the init
method is omitted, the class will inherit the init
method of its superclass (if any).
§Methods
Methods are functions that are defined inside a class. They can be called on instances of the
class. Methods can be defined by using the ()
operator after the method name. Methods can
take any number of arguments.
§Self Reference
Using this
keyword will refer to the instance of the class that the method is being called on.
§Inheritance
Using <
operator will create a new class with the left-hand side as the superclass.
The superclass is an optional object to another class that this class inherits from.
When inheriting from a superclass, the subclass will inherit all the methods of the superclass.
Prefixing a method call with super
will call the superclass’s method of the same name.
Implementations§
Source§impl Class
impl Class
Sourcepub fn new(
name: String,
superclass: Option<Object>,
methods: HashMap<String, Function>,
) -> Self
pub fn new( name: String, superclass: Option<Object>, methods: HashMap<String, Function>, ) -> Self
Creates a new class with the given name, superclass (if any), and methods.
Sourcepub fn get_method(&self, name: &str) -> Option<Function>
pub fn get_method(&self, name: &str) -> Option<Function>
Returns the method with the given name. If the method is not defined, it will return None
.
If the method is not defined in this class, it will inherit the method from its superclass
(if any).
Trait Implementations§
Source§impl Callable for Class
impl Callable for Class
Source§fn arity(&self) -> usize
fn arity(&self) -> usize
Returns the arity of the init
method of the class. If the init
method is not defined,
it will return 0.
Source§fn call(
&self,
interpreter: &mut Interpreter<'_>,
arguments: Vec<Object>,
) -> Result<Object, RuntimeError>
fn call( &self, interpreter: &mut Interpreter<'_>, arguments: Vec<Object>, ) -> Result<Object, RuntimeError>
Creates a new instance of the class and calls the init
method on it.
If the init
method is not defined, it will inherit the init
method of its superclass
(if any).