Limitations

C# and haXe are quite similar in most ways, which made CS2HX fairly easy to write. However, there are a few key gotchas that you need to be aware of.

Most limitations easy to work around by making minor adjustments to your C# code, however some limitations can be a bit annoying. For more guidance on how to work through converting your code to work with CS2HX, see the Getting Started guide.

Flat-out not supported: goto, yield, unsafe code, operator overloads, implicit cast operators.

CS2HX does not support any of these, and currently does not fix them for you. You must not use any of these constructs in your C# code.

Static Constructors

haXe does not support static constructor initialization the way C# does. If your C# code contains a static constructor, CS2HX will generate a cctor method for you in that class. You must call this method yourself before you access the class.

As a shortcut, CS2HX will generate a Constructors type that contains calls to all static methods it generated. You can simply call this method at your program start-up and all static constructors will be initialized.

haXe does not support structs

haXe does not allow you to define your own value types, only reference types. Any structures in you code will be a reference type in haXe, and this may cause bugs if your code assumes the structure is being copied by value.

To fix this, change your structs to classes or make the structs immutable. (Mutable structs are a bad idea in C# anyway.)

Overloaded constructors

haXe doesn't support function overloading. CS2HX will fix this for most functions for you by appending the type parameters to the function name. However, with constructors, the name can't be changed. Therefore, you should ensure that the types you're converting to haXe only have one constructor. A common solution to this is changing all but one to static creation functions.

Nullable types

When using nullable types (i.e. the "int?" type), you must always access the .Value and .HasValue properties instead of comparing the type itself.

For example, when comparing to null, these statements are equivilant in C#, but for CS2HX you must use the latter:
if (variable != null) ... //Don't do this
if (variable.HasValue) ... //Do this

"Using" statements must reference a local variable

CS2HX transforms your "using" statements to a try/catch block. Since the code in the using condition gets called beforehand in C# and afterwards in haXe, you can't put code with side-effects into the using condition. For example:

Don't do:
	using (SomeMethod()) { ... }

Instead, do:
	var someObject = SomeMethod();
	using (someObject) { ... }

Casts from float/double to int must be explicit

haXe cannot do integer division. Any division in haXe automatically produces floating point numbers. Therefore, to do integer division, you must be explicit.

Instead of doing:

	int i = someInt / someOtherInt;

Do this:

	int i = (int)(someInt / someOtherInt);

Dictionaries and HashSets

The Dictionary<> and HashSet<> generic types work a bit differently. .net's GetHashCode() methods will not be used when determining equality, therefore you should not use complex types as hash indexes. Keep your indexes to strings, numeric types, and Guids.

Cannot return from within "lock" or "using"

You cannot return from within a "lock" or "using" block. Instead, save your return value to a local variable and return it below the lock or using block.

Events

CS2HX does not support C# events. Instead, you can replace it with a delegate if you just need a single handler, or if you need multiple handlers you can write your own class in C# that manages a list of delegates.

Base class libraries

Of course, CS2HX does not implement every single method in .net's base class libraries. You should check the "system" directory to see what types and methods are supported. If you use something that's not implemented, you may have to implement that method or type yourself.

Last edited Oct 25, 2013 at 4:11 AM by fickerra, version 9

Comments

No comments yet.