Help - include directive for Haxe

99 views
Skip to first unread message

Jeff Ward

unread,
Oct 2, 2015, 5:17:15 PM10/2/15
to Haxe
Hi Haxe folk,

I'm interested in implementing the include directive in Haxe -- that is, to enable reuse of code blocks defined in some included file, such that the behavior is that those lines of code are literally pasted in the including file.

Two obvious options are 1) using a macro, or 2) having a pre-processor step. This was discussed long ago here on the mailing list, and I've started a repo with a macro-based solution per this conversation. But it doesn't quite work as well as I'd hope. On the other hand, a pre-processor would be harder to integrate into build workflows and might break things like code completion and error checking via the Haxe compiler, so it's less attractive unless those issues can be addressed.

I've outlined my specific issues with the macro solution in the issues section of my repo (also annotated in code snippets, e.g. defining functions.)

Surely some of you have given this some thought. Ideas? Feel free to discuss here or in the repo issue threads. Add issues, submit PRs, whatever. I'd like to see a good include solution for Haxe (or maybe there already is one I'm unaware of).

Thanks,
-Jeff

Philippe Elsass

unread,
Oct 3, 2015, 5:16:36 AM10/3/15
to Haxe

I'm afraid it is something that should be baked in the compiler as it should be evaluated when reading the raw source from the filesystem.

A macro approach that somehow works is to do like mpartial: combining valid Haxe classes.
https://github.com/massiveinteractive/mpartial
But honestly, we have abandoned using it because it is a very costly macro in large projects, and there are fundamental problems in practice - for instance a library uses mpartial and you need to override a combined class.

Back to the preprocessor approach, maybe it could work if you always work with processes files, and can reprocess every time to need:

//#startinclude ../version.hx
var version = 1.0; // from version.hx
//#endinclude

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

Juraj Kirchheim

unread,
Oct 5, 2015, 7:19:24 AM10/5/15
to haxe...@googlegroups.com
I think before you go into implementation details, you should take a moment to explain what you're trying to achieve.

If you want 100% include-like behavior, then you will need to use some preprocessor as Philippe pointed out. FWIW I'm quite confident it cannot be done with macros and don't think there is any real chance to convince Nicolas to support this in the compiler, when there's so many alternatives for code reuse in Haxe.

You've got it working for expressions, which I guess is the most interesting bit. Getting it to work for class fields would work with build macros, as someone else mentioned on the tracker. Getting it to work for whole classes seems weird. I don't get how this would work in the first place. Assume `A.hx` and `B.hx` both include `ClassFile.hx.incl` - you will get a duplicate type declaration and that's that. Also, there is the so called `import` directive, which works pretty well for reusing code from one module in another :P

Are you doing this mainly to satisfy your curiosity? Or do you have some use cases that might shed a light on which gaps in Haxe's existing code reuse mechanisms you are need to fill?

Best,
Juraj

Jeff Ward

unread,
Oct 5, 2015, 9:55:44 AM10/5/15
to Haxe
Thanks, Philippe. mpartial looks useful, I'll look into how it works.

Note - Brendon pointed out @:mergeBlock, which solves the issue of includes having a separate scope (so including files can access included variables.)

Hi Juraj. I just noted my primary usecase in an issue - I'm evaluating porting a large codebase from AS3 that uses includes to achieve code reuse outside inheritance.

Thanks,
-Jeff

Juraj Kirchheim

unread,
Oct 5, 2015, 11:35:24 AM10/5/15
to haxe...@googlegroups.com
On Mon, Oct 5, 2015 at 3:55 PM, Jeff Ward <jeff...@gmail.com> wrote:

Hi Juraj. I just noted my primary usecase in an issue - I'm evaluating porting a large codebase from AS3 that uses includes to achieve code reuse outside inheritance.
 
Ok, I see. What about using a preprocessor for the direct port and then refactoring the Haxe code to use Haxe's feature's instead?

I think you would be better off breaking down the different usages of #include and searching for suitable substitutes individually. Syntax level is fine. Something build macro based for multiple inheritance is also pretty straight forward (there's also a couple of libraries already doing it). The "include a whole class"-thing I still didn't understand. Could you provide a working AS3 example to explain what you're after?

Best,
Juraj

Jeff Ward

unread,
Oct 5, 2015, 11:47:55 AM10/5/15
to Haxe
> The "include a whole class"-thing I still didn't understand.

Yeah, it's not used in my use case, I was just curious that Nicolas had pointed it out.

Best,
-Jeff
Reply all
Reply to author
Forward
0 new messages