Some Haxe C# implementation questions

Hi guys, it’s been actually quite some time I’ve been around. Nowadays I started to play with hxcs to implement another target for my framework. However before I delve into the research which seems sporadic at least and will probably result in a lot of try and see. I have a few specific questions about using Haxe with C#

  1. Asynchronous stuff for example in Unity is quite a common occurrence how would you go on implementing code like this in haxe so it correctly transpiles to Unity C#?
    void Start()
    {
        StartCoroutine(GetRequest("https://www.example.com"));
    }

    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            yield return webRequest.SendWebRequest();

            string[] pages = uri.Split('/');
            int page = pages.Length - 1;

            if (webRequest.isNetworkError)
            {
                Debug.Log(pages[page] + ": Error: " + webRequest.error);
            }
            else
            {
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
        }
    }
  1. Is there a way to inject a block of C# code into a haxe code? Instead of using multiple untyped calls?

  2. What is the best way to call external C# code from within haxe code? Keeping in mind that in most cases I want to get a return value as well.

So if anyone can at least give me a hint it will definitely save me quite some time that I can spend on coding the actual framework and bringing haxe evangelism even further.

Thanks.

you can use the -net-lib option to include any DLL. Haxe will parse the DLL and allow you to use the types from the DLL in Haxe code.

For more details you can check the manual: .NET version and external libraries - Haxe - The Cross-platform Toolkit

Thanks I am well aware of the -net-lib it would be downright impossible to use Unity with haxe if there wasn’t a way to use unityengine.dll however it doesn’t really answer any of the questions.

Unless you propose that I write down the code in question 1. outside of haxe and then use it inside haxe. Which is something I was thinking about as a last resort basically having all the async stuff in a unity plugin refactoring it into the classic event driven model javascript or as3 use.

The -net-lib option could be an answer to your question 3, but you’d have to compile that external C# code into a dll.

Classic haxe externs could be another way, that’s what I use (but in my configuration haxe only generates C# code, and I compile with dotnet cli; I don’t know if that matters or not for externs).

See Injecting raw C# code - Haxe - The Cross-platform Toolkit for C# code injection. You might want to use @:functionCode, which works fairly well, even with type parameters.

As for your first question, there are multiple approaches I guess. I wouldn’t know what to recommend, though.

I think @nadako uses haxe + unity, maybe he could help?

yield in C# transforms the function into a reentrant state machine (just like async/await) aka coroutine. Haxe does not natively support coroutines at the moment (although it’s planned), but as usual “there’s a macro for that!” :slight_smile: IIRC there is one macro exactly for C#/Unity in the unihx project. It might be outdated but still a good start.

And -net-lib is just a fancy automatic extern loader. You can define the same extern types by hand, so you don’t technically need it to interop with C# code.

Thanks nadako I will check the macro, so far I’ve split my implementation into a managed dll plugin that handles all the coroutine stuff and haxe part which goes without.

Also: GitHub - RealyUniqueName/Coro: Async/await, generators, and arbitrary coroutines for Haxe.