# Some Haxe C# implementation questions

**URL:** <https://community.haxe.org/t/some-haxe-c-implementation-questions/1630>\
**Category:** Haxe\
**Tags:** haxe-cs\
**Created:** [March 27, 2019, 8:17pm UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630 "2019-03-27T20:17:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pshtif](https://community.haxe.org/user_avatar/community.haxe.org/pshtif/32/783_2.png) [@pshtif](https://community.haxe.org/u/pshtif)\
**Post date:** [March 27, 2019, 8:17pm UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/1 "2019-03-27T20:17:13Z")

</div>

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#?

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

---

<div class="post-metadata">

**Author:** ![kevinresol](https://community.haxe.org/user_avatar/community.haxe.org/kevinresol/32/272_2.png) [@kevinresol](https://community.haxe.org/u/kevinresol)\
**Post date:** [March 28, 2019, 7:07am UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/2 "2019-03-28T07:07:04Z")

</div>

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](https://haxe.org/manual/target-cs-external-libraries.html)

---

<div class="post-metadata">

**Author:** ![pshtif](https://community.haxe.org/user_avatar/community.haxe.org/pshtif/32/783_2.png) [@pshtif](https://community.haxe.org/u/pshtif)\
**Post date:** [March 28, 2019, 9:15am UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/3 "2019-03-28T09:15:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)\
**Post date:** [March 28, 2019, 9:23am UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/4 "2019-03-28T09:23:31Z")

</div>

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](https://haxe.org/manual/target-cs-code-injection.html) 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?

---

<div class="post-metadata">

**Author:** ![nadako](https://community.haxe.org/user_avatar/community.haxe.org/nadako/32/5_2.png) [@nadako](https://community.haxe.org/u/nadako)\
**Post date:** [March 31, 2019, 8:55pm UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/5 "2019-03-31T20:55:35Z")

</div>

`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!” 🙂 IIRC there is one macro exactly for C#/Unity in the [unihx](https://github.com/unihx/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.

---

<div class="post-metadata">

**Author:** ![pshtif](https://community.haxe.org/user_avatar/community.haxe.org/pshtif/32/783_2.png) [@pshtif](https://community.haxe.org/u/pshtif)\
**Post date:** [April 4, 2019, 11:14am UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/6 "2019-04-04T11:14:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fullofcaffeine](https://community.haxe.org/user_avatar/community.haxe.org/fullofcaffeine/32/28_2.png) [@fullofcaffeine](https://community.haxe.org/u/fullofcaffeine)\
**Post date:** [April 4, 2019, 11:56pm UTC](https://community.haxe.org/t/some-haxe-c-implementation-questions/1630/7 "2019-04-04T23:56:59Z")

</div>

Also: [GitHub - RealyUniqueName/Coro: Async/await, generators, and arbitrary coroutines for Haxe](https://github.com/RealyUniqueName/Coro).
