You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support passing float[] (Single[]) to JS from .NET via [JSImport] #97633
Is your feature request related to a problem? Please describe the problem.
I recently tried (and failed) to pass a simple float[] from .NET to JS. This is obviously not supported, as per the docs:
I got it working by casting my floats to doubles using Linq, quite an ugly way which probably could do in a better way: var myDoubles = myFloats.Select(f => (double)f).ToArray();
passing this to JS works.
Describe the solution you'd like
It struck me, if the "workaround" is this simple, why could it not be indirectly supported by the [JSImport] API itself? Either through casting floats to doubles or something more clever? I mean, whatever we can fit in a float, could most definately fit in a double. If you want to explicit then force us to use [JSMarshalAs<JSType.Array<JSType.Number>>] or something to make it work.
I guess float[] floatValues = Array.ConvertAll(values, Convert.ToSingle); would be fastest.
Ok. Thanks, sounds good. I guess your example in my case would be something like double[] doubleValues = Array.ConvertAll(floatValues, Convert.ToDouble);
as my values are float but they need to be double for it to work, as it is today.
Activity
maraf commentedon Jan 30, 2024
cc @pavelsavara
pavelsavara commentedon Jan 30, 2024
Indeed, there is duplicate feature request for that. #97380
I found few ways how you can do it here https://stackoverflow.com/questions/69805135/how-does-c-sharp-convert-double-arrays-to-float-arrays-or-int-arrays
I guess
float[] floatValues = Array.ConvertAll(values, Convert.ToSingle);would be fastest.Matheos96 commentedon Jan 30, 2024
Ok. Thanks, sounds good. I guess your example in my case would be something like
double[] doubleValues = Array.ConvertAll(floatValues, Convert.ToDouble);as my values are
floatbut they need to bedoublefor it to work, as it is today.