Extending the Haxe Standard Library

63 views
Skip to first unread message

Ashiq A.

unread,
Apr 28, 2016, 12:27:18 PM4/28/16
to haxe...@googlegroups.com
Hi,

I'm in the process of writing (yet another) "extension" to the Haxe standard library. I ran into a problem with static classes (eg. File). I can't extend them with static extensions (I don't remember what it's called in Haxe), because it's static.

This probably rules out the "ideal" API of someone writing "import haxe.io.File" and getting my extensions (if they have my haxelib). This is how Linq works in C#, as a comparative reference point.

My questions:

1) Is there some macro magic or something I can do to make this work the way I want?
2) How did other libraries (eg. thx.core) handle this?

Another plausible alternative is to tell my users to "switch from haxe.io.File to myawesomelib.io.File," but that requires redirecting all the calls to the standard lib. I recall reading that there is some macro magic for this. 

What's the best way to do this?

--Ashiq

Mark Knol

unread,
Apr 28, 2016, 12:34:41 PM4/28/16
to Haxe
You can just create a file called sys.io.File in your classpath. That will overwrite the one in the standard library. Take a look at hxnodejs to see how they add filesystem access for nodejs.

Ashiq A.

unread,
Apr 28, 2016, 12:55:16 PM4/28/16
to haxe...@googlegroups.com
I see, that's interesting. My case is slightly different: I want to carry along all the existing methods, not rewrite them.

Also, can you please explain:

- What @:dce does
- What @:coreApi does

Thanks,



On Thu, Apr 28, 2016 at 12:34 PM, Mark Knol <mark...@gmail.com> wrote:
You can just create a file called sys.io.File in your classpath. That will overwrite the one in the standard library. Take a look at hxnodejs to see how they add filesystem access for nodejs.

--
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.

Simon Krajewski

unread,
Apr 28, 2016, 1:06:09 PM4/28/16
to haxe...@googlegroups.com
Am 28.04.2016 um 18:26 schrieb Ashiq A.:
> Hi,
>
> I'm in the process of writing (yet another) "extension" to the Haxe
> standard library. I ran into a problem with static classes (eg. File).
> I can't extend them with static extensions (I don't remember what it's
> called in Haxe), because it's static.

I think you can declare static extensions with a first argument of
Class<File>.

Simon

Ashiq A.

unread,
Apr 28, 2016, 1:20:23 PM4/28/16
to haxe...@googlegroups.com
It doesn't seem to compile for me.  Am I doing something wrong?

Try.haxe example: http://try.haxe.org/#7BBc8

Ashiq A.

unread,
Apr 28, 2016, 2:20:51 PM4/28/16
to haxe...@googlegroups.com
My bad. It looks like there are some very specific things that need to line up:

- You need (as usual) "using MyWhateverClass"
- The static extension method should be public (the default visibility isn't enough)
- The first parameter is, as you said, Class<File>

I'll open a PR to update the Haxe docs with an example for this. IMO this is really, really, really useful.

Thanks for the help!

Reply all
Reply to author
Forward
0 new messages