rhai/doc/src/language/object-maps-oop.md

41 lines
1.7 KiB
Markdown
Raw Normal View History

2020-06-26 10:39:18 +08:00
Special Support for OOP via Object Maps
======================================
{{#include ../links.md}}
2020-06-27 17:34:39 +08:00
[Object maps] can be used to simulate [object-oriented programming (OOP)][OOP] by storing data
2020-06-26 10:39:18 +08:00
as properties and methods as properties holding [function pointers].
2020-06-26 19:32:06 +08:00
If an [object map]'s property holds a [function pointer], the property can simply be called like
a normal method in method-call syntax. This is a _short-hand_ to avoid the more verbose syntax
of using the `call` function keyword.
When a property holding a [function pointer] is called like a method, what happens next depends
on whether the target function is a native Rust function or a script-defined function.
If it is a registered native Rust method function, then it is called directly.
If it is a script-defined function, the `this` variable within the function body is bound
to the [object map] before the function is called. There is no way to simulate this behavior
via a normal function-call syntax because all scripted function arguments are passed by value.
2020-06-26 10:39:18 +08:00
```rust
2020-07-19 17:14:55 +08:00
fn do_action(x) { this.data += x; } // 'this' binds to the object when called
2020-06-26 10:39:18 +08:00
let obj = #{
data: 40,
2020-07-19 17:14:55 +08:00
action: Fn("do_action") // 'action' holds a function pointer to 'do_action'
2020-06-26 10:39:18 +08:00
};
2020-07-19 17:14:55 +08:00
obj.action(2); // Calls 'do_action' with `this` bound to 'obj'
2020-06-26 19:32:06 +08:00
2020-07-19 17:14:55 +08:00
obj.call(obj.action, 2); // The above de-sugars to this
2020-06-26 10:39:18 +08:00
2020-07-19 17:14:55 +08:00
obj.data == 42;
// To achieve the above with normal function pointer call will fail.
fn do_action(map, x) { map.data += x; } // 'map' is a copy
obj.action.call(obj, 2); // 'obj' is passed as a copy by value
2020-06-26 10:39:18 +08:00
```