COMMUNITY

Float seems to be double for C# target, how about float externs?

haxe-cs
(Peter ) #1

What is the correct way to write externs for C# libraries that actually use real floats? All the implementations I tried like abstracts always end up with a lot of allocation in C# at runtime due to the conversion.

Is there a way to use single precision in Haxe for C#?

0 Likes

(Dan Korostelev) #2

Have you tried Single?

0 Likes

(Peter ) #3

Thanks for the fast response nadako, I’ve tried to avoid it as it just means the problem will go one level up. To put more context into my problem is that the whole framework is written using haxe Floats so if I type externs using Single the high level framework will still use Floats and therefore there will be a problem to go from Float to Single.

I assumed this would result in similar issue as writing abstracts over it and doing this.x = x where this.x is native float and x is haxe Float? Which means a lot of allocation going on? Or am I wrong?

0 Likes

(Peter ) #4

Tried a quick test and sending Float from high level class to extern using Single will result in cannot convert from “haxe.lang.Null” to “float” so it seems it doesn’t even do it implicitly like when using abstracts.

0 Likes

(Peter ) #5

There seems to be an issue with default parameters, Haxe always transpiles them as global::haxe.lang.Null for some reason even though they are not using the questionmark ?p_parameter = 0 syntax. Shouldn’t optional parameters like p_parameter = 0 be transpiled as the native nonnullable type?

0 Likes

(Peter ) #6

It is actually getting even more complicated due to this default parameter issue Reflection doesn’t work on specific types with such constructors in C#.

For example this code will crash:

class Something {
   public new(p_value:Float = 0) {}
}

Type.createInstance(Something, [0.0]);

Because it will assume that the float is not directly assignable to the costructor parameter as it is a nonullable type on static target. And it will end up with EmptyObject as the most rated method inside reflection. Which will crash as:

Invalid cast from ‘System.Double’ to ‘haxe.lang.EmptyObject’

0 Likes