rhai/doc/src/language/oop.md

44 lines
1.5 KiB
Markdown
Raw Normal View History

2020-06-26 04:39:18 +02:00
Object-Oriented Programming (OOP)
================================
{{#include ../links.md}}
Rhai does not have _objects_ per se, but it is possible to _simulate_ object-oriented programming.
Use [Object Maps] to Simulate OOP
--------------------------------
Rhai's [object maps] has [special support for OOP]({{rootUrl}}/language/object-maps-oop.md).
| Rhai concept | Maps to OOP |
| ----------------------------------------------------- | :---------: |
| [Object maps] | objects |
| [Object map] properties holding values | properties |
| [Object map] properties that hold [function pointers] | methods |
2020-07-19 11:14:55 +02:00
When a property of an [object map] is called like a method function, and if it happens to hold
a valid [function pointer] (perhaps defined via an [anonymous function]), then the call will be
dispatched to the actual function with `this` binding to the [object map] itself.
2020-06-26 04:39:18 +02:00
Examples
--------
```rust
// Define the object
2020-07-19 11:14:55 +02:00
let obj =
#{
data: 0,
increment: |x| this.data += x, // when called, 'this' binds to 'obj'
update: |x| this.data = x, // when called, 'this' binds to 'obj'
action: || print(this.data) // when called, 'this' binds to 'obj'
};
2020-06-26 04:39:18 +02:00
// Use the object
obj.increment(1);
obj.action(); // prints 1
obj.update(42);
obj.action(); // prints 42
```