2020-06-20 06:06:17 +02:00
|
|
|
Override a Built-in Function
|
|
|
|
===========================
|
|
|
|
|
|
|
|
{{#include ../links.md}}
|
|
|
|
|
2020-12-26 16:21:16 +01:00
|
|
|
Any similarly-named function defined in a script _overrides_ any built-in or registered
|
2020-06-20 06:06:17 +02:00
|
|
|
native Rust function of the same name and number of parameters.
|
|
|
|
|
|
|
|
```rust
|
2020-12-26 16:21:16 +01:00
|
|
|
// Override the built-in function 'to_float' when called as a method
|
|
|
|
fn to_float() {
|
|
|
|
print("Ha! Gotcha! " + this);
|
|
|
|
42.0
|
2020-06-20 06:06:17 +02:00
|
|
|
}
|
|
|
|
|
2020-12-26 16:21:16 +01:00
|
|
|
let x = 123.to_float();
|
2020-07-26 04:07:40 +02:00
|
|
|
|
2020-12-26 16:21:16 +01:00
|
|
|
print(x); // what happens?
|
2020-06-20 06:06:17 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
A registered native Rust function, in turn, overrides any built-in function of the
|
|
|
|
same name, number and types of parameters.
|