js extern help

52 views
Skip to first unread message

wighawag

unread,
Apr 18, 2017, 9:18:48 AM4/18/17
to haxe...@googlegroups.com
Hi,

I have a js library used this way in javascript:

```
var lib = require("@lib/lib.js")

var Api = lib.Api;
var t = new Api.Test.Http('http://localhost:8000'); // t is of type Test
var api = new Api(t);
...
```

I was going to create an extern class for Api and Test but I do not know how to deal with the fact that Api is both a class and kind of a namespace

In Haxe I would like it to be something like :

```
import lib.Api;
//not sure how I would import a reference to Http but then it would be something like
var t = new Http('http://localhost:8000');
var api = new Api(t);
...
```

 but could not figure it how I could apply to my case :

Thanks

wighawag

unread,
Apr 18, 2017, 9:34:12 AM4/18/17
to haxe...@googlegroups.com
I actually managed to get something with the following


in lib/Api.hx
```
package lib;

import lib.api.Test;

@:jsRequire("@lib/lib.js", "Api")
extern class Api{
public function new(test : Test);
}

```


in lib/api/Test.hx
```
package lib.api;

@:jsRequire("@lib/lib.js", "Api.Test")
extern class Test{

}

@:jsRequire("@lib/lib.js", "Api.Test.Http")
extern class Http extends Test{
public function new(url : String);
}
```

this get then used this way :

```
import lib.Api;
import lib.api.Test.Http;

class TestAll{
public static function main(){
var t = new Http('localhost:8545');
var api = new Api(t);
}
}
```

What do you think ? Is it idiomatic in haxe js world ? Can it be better ?

clemos

unread,
Apr 18, 2017, 9:39:01 AM4/18/17
to haxe...@googlegroups.com
Hi,

I think you should do that :
---
@:jsRequire('@lib/lib.js'), 'Api')
extern class Api {
...
}
---
package api.test;
@:jsRequire('@lib/lib.js'), 'Api','Test','Http')
extern class Http {
...
}
---
and finally:
---
import api.test.Http;
import Api;
// your code
---

In other words, I think your haxe extern shouldn't bother too much
that `Test` is actually a member of `Api`.
You might think there is an overhead in calling `require` twice
instead of once, but in practice it is generally negligible.

Best,
Clément
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

wighawag

unread,
Apr 18, 2017, 11:48:29 AM4/18/17
to haxe...@googlegroups.com
Thanks, it is not always obvious how to deal with some extern

Jeff Ward

unread,
Apr 18, 2017, 1:10:21 PM4/18/17
to Haxe
Another thought... I haven't used @:jsRequire, but I have used @:native, and it is also useful for declaring externs, even ones nested in various objects/"namespaces" as in your case.

Here's an example: https://try.haxe.org/#36131

@:native('window.location')
extern class Location
{
    public static var href:String;
    public static var search:String;
    public static var hash:String;
}

We declare a Location API that just so happens to be buried in the window object. But our .hx code doesn't need to know this. And while this one example is browser specific, the technique in general is not.
Reply all
Reply to author
Forward
0 new messages