Add dynamic dispatch.

This commit is contained in:
Stephen Chung
2020-06-25 19:22:14 +08:00
parent 580a62daaf
commit 259b6d0fcf
3 changed files with 81 additions and 28 deletions

View File

@@ -7,8 +7,8 @@ In fact, internally a function pointer simply stores the _name_ of the function
Call a function pointer using the `call` method, which needs to be called in method-call style.
Built-in Functions
------------------
Built-in methods
----------------
The following standard methods (mostly defined in the [`BasicFnPackage`]({{rootUrl}}/rust/packages.md) but excluded if
using a [raw `Engine`]) operate on [strings]:
@@ -54,3 +54,54 @@ let hello = Fn(fn_name + "_world");
hello.call(0); // error: function not found - "hello_world (i64)"
```
Dynamic Dispatch
----------------
The purpose of function pointers is to enable rudimentary _dynamic dispatch_, meaning to determine,
at runtime, which function to call among a group.
Although it is possible to simulate dynamic dispatch via a number and a large `if-then-else-if` statement,
using function pointers significantly simplifies the code.
```rust
let x = some_calculation();
// These are the functions to call depending on the value of 'x'
fn method1(x) { ... }
fn method2(x) { ... }
fn method3(x) { ... }
// Traditional - using decision variable
let func = sign(x);
// Dispatch with if-statement
if func == -1 {
method1(42);
} else if func == 0 {
method2(42);
} else if func == 1 {
method3(42);
}
// Using pure function pointer
let func = if x < 0 {
Fn("method1")
} else if x == 0 {
Fn("method2")
} else if x > 0 {
Fn("method3")
}
// Dynamic dispatch
func.call(42);
// Using functions map
let map = [ Fn("method1"), Fn("method2"), Fn("method3") ];
let func = sign(x) + 1;
// Dynamic dispatch
map[func].call(42);
```