# Rest arguments for callback

**URL:** <https://community.haxe.org/t/rest-arguments-for-callback/2534>\
**Category:** Haxe\
**Tags:** haxe-js, extern\
**Created:** [June 23, 2020, 3:12pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534 "2020-06-23T15:12:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RedHeadEatBread](https://community.haxe.org/user_avatar/community.haxe.org/redheadeatbread/32/894_2.png) [@RedHeadEatBread](https://community.haxe.org/u/RedHeadEatBread)\
**Post date:** [June 23, 2020, 3:12pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/1 "2020-06-23T15:12:07Z")

</div>

There is a js function

```haxe
export function callback(event: string, listener: (...args: any[]) => void): void;

```

I tried to wrap this in an extern class

```haxe
function callback(event:String, listener:Rest<Dynamic>->Void):Void;

```

But when you try to call it

```haxe
callback('someEvent', myHandler);

function myHandler(arg:String) {
  //...
}

```

I get a compiler error

`error: haxe.extern.Rest<Dynamic> should be String`

This is not supported and I have to list all the possible options in @: overload metadata? Or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [June 23, 2020, 3:17pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/2 "2020-06-23T15:17:39Z")

</div>

Because `listener` is `Rest<Dynamic>->Void`, but `myHandler` is `String->Void`.  
`Rest<T>` allows to _call_ a function with variable set of arguments.  
If you need to allow a _function reference_ with any set of arguments, try changing `listener` type to `haxe.Constraints.Function`

---

<div class="post-metadata">

**Author:** ![RedHeadEatBread](https://community.haxe.org/user_avatar/community.haxe.org/redheadeatbread/32/894_2.png) [@RedHeadEatBread](https://community.haxe.org/u/RedHeadEatBread)\
**Post date:** [June 23, 2020, 4:01pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/3 "2020-06-23T16:01:04Z")

</div>

I thought about it, but it will accept any function. The js code I want to wrap has many functions like this

```haxe
export function callback (event: string, listener: (... args: string []) => boolean | void): void;

```

Can I somehow wrap this in an extern safe manner?

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [June 23, 2020, 4:25pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/4 "2020-06-23T16:25:56Z")

</div>

You can also change `myHandler` to `function myHandler(args:Rest<String>) {`

---

<div class="post-metadata">

**Author:** ![RedHeadEatBread](https://community.haxe.org/user_avatar/community.haxe.org/redheadeatbread/32/894_2.png) [@RedHeadEatBread](https://community.haxe.org/u/RedHeadEatBread)\
**Post date:** [June 23, 2020, 4:30pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/5 "2020-06-23T16:30:00Z")

</div>

It is assumed that the handler will be declared in the regular class (not extern).

---

<div class="post-metadata">

**Author:** ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)\
**Post date:** [June 23, 2020, 4:55pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/6 "2020-06-23T16:55:19Z")

</div>

Unfortunately I don’t see a clean solution.There’s an ongoing effort [Rest arguments by RealyUniqueName · Pull Request #9274 · HaxeFoundation/haxe · GitHub](https://github.com/HaxeFoundation/haxe/pull/9274) But until it’s finished you’ll have to resort to `haxe.Constraints.Function`.

---

<div class="post-metadata">

**Author:** ![RedHeadEatBread](https://community.haxe.org/user_avatar/community.haxe.org/redheadeatbread/32/894_2.png) [@RedHeadEatBread](https://community.haxe.org/u/RedHeadEatBread)\
**Post date:** [June 23, 2020, 5:17pm UTC](https://community.haxe.org/t/rest-arguments-for-callback/2534/7 "2020-06-23T17:17:56Z")

</div>

I get it. Thanks for the help. You guys are doing a great job!
