rhai/doc/src/language/overload.md

30 lines
833 B
Markdown
Raw Normal View History

2020-06-20 06:06:17 +02:00
Function Overloading
===================
{{#include ../links.md}}
2020-06-29 17:55:28 +02:00
[Functions] defined in script can be _overloaded_ by _arity_ (i.e. they are resolved purely upon the function's _name_
2020-06-20 06:06:17 +02:00
and _number_ of parameters, but not parameter _types_ since all parameters are the same type - [`Dynamic`]).
New definitions _overwrite_ previous definitions of the same name and number of parameters.
```rust
2020-07-28 04:25:57 +02:00
fn foo(x,y,z) { print("Three!!! " + x + "," + y + "," + z); }
2020-06-25 05:07:56 +02:00
2020-07-28 04:25:57 +02:00
fn foo(x) { print("One! " + x); }
2020-06-25 05:07:56 +02:00
2020-07-28 04:25:57 +02:00
fn foo(x,y) { print("Two! " + x + "," + y); }
2020-06-25 05:07:56 +02:00
2020-07-28 04:25:57 +02:00
fn foo() { print("None."); }
2020-06-25 05:07:56 +02:00
2020-07-28 04:25:57 +02:00
fn foo(x) { print("HA! NEW ONE! " + x); } // overwrites previous definition
2020-06-20 06:06:17 +02:00
foo(1,2,3); // prints "Three!!! 1,2,3"
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
foo(42); // prints "HA! NEW ONE! 42"
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
foo(1,2); // prints "Two!! 1,2"
2020-06-25 05:07:56 +02:00
2020-06-20 06:06:17 +02:00
foo(); // prints "None."
```