HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.17k stars 654 forks source link

Python target overly mangles class names, removes modules #8271

Open thomasjwebb opened 5 years ago

thomasjwebb commented 5 years ago

I understand the historical and practical reasons for the design of the python target. The code runs perfectly fine if my aim is to make a python file to feed into the interpreter. However, as all code is placed in the same file, namespaces and classes are all concatenated together. If there was an option to output multiple files in appropriate directories, like --py-separate-files, then the class and namespace names can remain true to the original (python works similar to haxe in this regard) and the output can be more suitable to ship as a pip package. If this is a change that you all would be interested in but too busy to implement yourselves I may be interested in learning ocaml in order to make a PR for this.

kevinresol commented 5 years ago

If anyone is going to implement this, please keep in mind that this is also applicable to js and lua.

fullofcaffeine commented 5 years ago

With JS, this can be solved currently by using a custom JS generator. Not the most optimal solution in terms of performance, but it works quite well (see: https://github.com/kevinresol/hxgenjs). Would it be possible to expose a similar API for Python?

thomasjwebb commented 5 years ago

It would be great if the js target had an option to behave like hxgenjs because I think it's a common enough use case but it's great that the js target at least has a hook for making custom generators. And as @kevinresol mentioned to me earlier, I could still use the js target to generate python in that way but I'd at least need to also add --macro include('python') to make that work right. So if python at least had that same hook as js, that would be an improvement.

Ideally the haxe compiler itself should be able to make code suitable for use by developers in the target language for targets where that's feasible. At least for python I think a separate files option would make a huge difference.

nadako commented 5 years ago

While I agree that it would be nice to have some flexibility by default, one possible solution available right now would be to have an onGenerate macro that iterates over the types you want to expose and just generates some .py files that import the main file and re-export specific parts of it.

Aurel300 commented 5 years ago

Given that this is already done for C++, C#, Java, PHP, it seems like it could be a common infrastructure?

nadako commented 5 years ago

it could be a common infrastructure

Generating multiple files is not rocket science, the issue is that the way targets process modules might differ a lot (esp. with regard to circular dependencies).

fullofcaffeine commented 5 years ago

While I agree that it would be nice to have some flexibility by default, one possible solution available right now would be to have an onGenerate macro that iterates over the types you want to expose and just generates some .py files that import the main file and re-export specific parts of it.

Interesting idea 👍🏻

thomasjwebb commented 5 years ago

I'd like this to be streamlined by default. I know I can solve the problem once for myself but that's not really my point. I think haxe is close to being a good tool for making frameworks, not just applications, for multiple targets.

fullofcaffeine commented 5 years ago

I'd like this to be streamlined by default. I know I can solve the problem once for myself but that's not really my point. I think haxe is close to being a good tool for making frameworks, not just applications, for multiple targets.

Agreed.

This has been discussed multiple times in the past. In fact, there's an open issue that discusses this but in the context of the js target: https://github.com/HaxeFoundation/haxe/issues/5831. Perhaps we should continue discussing there but expand it to include all targets?

On Fri, May 10, 2019 at 4:35 PM Thomas J. Webb notifications@github.com wrote:

I'd like this to be streamlined by default. I know I can solve the problem once for myself but that's not really my point. I think haxe is close to being a good tool for making frameworks, not just applications, for multiple targets.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/HaxeFoundation/haxe/issues/8271#issuecomment-491437037, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAT2YFVJFBC5Y5S55BYFWLPUXTATANCNFSM4HMFANEQ .