Can Haxe compiler convert any C# code to Haxe?
Asked Answered
S

3

4

First I am super new to Haxe, so I am asking super noob question here, and I have 2 of them.

1) My first question is I know Haxe compiler can convert from Haxe to any other language (supported) but can it convert source code in other languages(supported) to Haxe?

2) If it can do so how much is its capability like can it only convert a few files lets say in C# to Haxe or if I give a huge repo like lets say this https://github.com/dotnet/corefx or https://github.com/tensorflow/tensorflow will it convert everything in it (ya I know this is too much to ask, but anyways)??

Selfliquidating answered 29/4, 2017 at 18:34 Comment(2)
No, nobody writes a compiler that translates code two ways. Someone may have written a C# to Haxe compiler, but that is definitely not a goal of the main projectLargess
tardisgo.github.io attempts to transpile code between Haxe and Go so some people do try to do such things, if Haxe became really big I am sure it would happen. But with c# use -cs-lib see my other answer.Dukas
A
9

No, the Haxe compiler only transpiles in one direction, which is:

Haxe Code ⭢ Selected Target

For the other direction, you'd need an external tool independent of the Haxe compiler. Over the years, several such tools were created (some abandoned):

There's also some tools for generating externs, rather than compiling source-to-source:

Adkisson answered 29/4, 2017 at 18:49 Comment(0)
D
4

Gama11 answer is correct but -net-lib and externs should be added since they do allow you to use c#/mono code in your Haxe c#/mono project.

Haxe can consume compiled code for all targets using externs ( compiler definitions ) for use within your Haxe project for that same target, for most targets you need to create suitable externs manually. However for some targets ( Java, C#, Flash ) Haxe can auto generate the externs required, although not always successful it can make it fairly painless to use existing libraries. I have found this very successful with flash and had varied success with Java target but it is always improving, and perhaps tried it once or twice with mono ( c# target ). In your hxml complier code you may write.

-swf-lib my.swf
-java-lib my.jar
-net-lib my.dll

You cannot use a a swf or jar file with a c# project, nor a dll with a flash or java project, atleast not this way, although you can use a swf with openfl and nme when targetting c++ via special parsing libaries.

Technically for C# this will be described in the future Haxe manual more formally than I have, see link to github markdown page for full details:

"Haxe can directly load .NET assembly files (.DLL) and convert its type definitions for use as Haxe types. To load a .NET assembly, use -net-lib library.dll compiler option. Haxe will then automatically parse types defined in that assembly file and make them available for import as Haxe types." - Quote https://github.com/HaxeFoundation/HaxeManual/wiki/Haxe-C%23

Dukas answered 29/4, 2017 at 23:3 Comment(0)
R
0

What is your intention? Are you:

  1. Trying to your logic written in C# code in Haxe project targeting C#?
  2. Trying to your logic written in C# code in Haxe project targeting non-C#?

For 1 you can use -net-lib so you can use the C# library directly in Haxe, so you don't need to convert the C# code to Haxe at all.

For 2 you have to port the C# code to Haxe code manually, or with some tools mentioned by Gama11. Haxe itself does not support such conversion.

Resourceful answered 1/5, 2017 at 1:50 Comment(1)
I want to write code for deep learning using tensor-flow, but I need to learn python to do so. The language I know well is JavaScript and C#. So I had a look at Haxe and I thought it can convert any language to any language and backwards which is wrong as Gama11 mentioned. So ya that was my main goal. Is it possible to use a library written in another language with a language of our choice?Selfliquidating

© 2022 - 2024 — McMap. All rights reserved.