Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added overload and rest arguments section #496

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions content/02-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,77 @@ Default values in Haxe are not part of the type and are not replaced at the call
This should be considered in performance-critical code where a solution without default values may sometimes be more viable.


<!--label:types-function-overloads-->
#### Overloads

Functions can be overloaded by attaching `extern inline overload` to its siganture. Extern functions don't generate a body, while inline makes extern
MrcSnm marked this conversation as resolved.
Show resolved Hide resolved
able to define the function body, so they're really necessary.

```haxe
class Test {
static function main() {
trace(add(50, 100));
trace(add(50.50, 100));
}

public static extern inline overload function add(a : Float, b : Float) : Float
{
trace("Float!");
return a + b;
}
public static extern inline overload function add(a : Int, b : Int) : Int
{
trace("Int!");
return a + b;
}
}

```

This will print
```
Test.hx:14:,Int!
Test.hx:3:,150
Test.hx:9:,Float!
Test.hx:4:,150.5
```

Function overloading by using the extern keyword were introduced in Haxe 4.2.0;

<!--label:types-function-rest-arguments-->
#### Rest Arguments

Functions can receive a list of arguments without specifying the array syntax by using the following syntax: `...varName`:

```haxe

class Test
{
static function main()
{
logAll("hello", "world", "rest", "arguments");
}
static function logAll(...arguments : String)
{
for(arg in arguments)
trace(arg);
}
}
```

Output:
```
Test.hx:10:,hello
Test.hx:10:,world
Test.hx:10:,rest
Test.hx:10:,arguments
```

Notice that it is also possible to use spread syntax: `logAll(...["hello", "world"]);`

Rest arguments were introduced in Haxe 4.2.0.

For further information on Rest arguments and Rest operator, check the [documentation](https://api.haxe.org/haxe/Rest.html)

<!--label:types-dynamic-->
### Dynamic
Expand Down