Description
I propose we include support for the CLR's new native numbers and C# binding.
I propose:
- Ensuring that explicit conversion from all of F#'s primitive types is supported
- Support arithmetic operators
- Support generic numeric functions (
max
,sqrt
, etc.) - Perhaps, a new manifest constant suffix to designate native. (Or is this already taken up by IntPtr?)
The existing way of approaching this problem in F# is:
-
Xamarin has introduced their own native types (
nint
,nuint
,nfloat
) in theSystem
namespace which are usable but inelegant from F# since they rely heavily on implicit conversions and some constructors are private/internal. -
F# has native int but it is incompatible with Xamarin's current work and the CLR's future work.
Pros and Cons
The advantages of making this adjustment to F# are that F# can be used for mobile development.
The disadvantage of making this adjustment to F# is it introduces yet another numeric type, one that can be potentially confused with existing support for System.IntPtr
.
Extra informtion
Estimated cost (XS, S, M, L, XL, XXL): S/M (depends on how good we make it)
If you browse Xamarin F# code for native int use, you will see the current state of the art. I think we can do better!
Here is an example of some code using it that isn't too bad: https://gist.github.com/praeclarum/c9e2d1a0f1089cb4025a
But things get much nastier if we want to support arithmetic with some constants or other variables.
let x = 42.0
let y = 3.14
let point = CGPoint (nfloat x, nfloat y) + CGPoint (nfloat 0.0, nfloat 1.0)
let angle = atan2 (float point.Y) (float point.X) |> nfloat
As you can see, F# code ends up being littered with types everywhere. In C#, this would be:
var x = 42;
var y = 3.14;
var point = new CGPoint (x, y) + new CGPoint (0, 1);
var angle = Math.Atan2 (point.Y / point.X);
Things basically go bad when you start mixing your model data (real data that is platform independent) with the data that native platforms plan to display (which is usually native sized). Problems compound when you start using native data structures (CGPoint) and try to do math with them.
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
- This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
- I have searched both open and closed suggestions on this site and believe this is not a duplicate
- This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
- This is not a breaking change to the F# language design
- I would be willing to help implement and/or test this
- I or my company would be willing to help crowdfund F# Software Foundation members to work on this