fsharp / fsharp/fslang-suggestions

Native Number Support and Improvements

Open
#562 10 comments 0 reactions 0 assignees View on GitHub
approved-in-principle needs rfc
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we include support for the [CLR's new native numbers and C# binding](https://github.com/dotnet/corefxlab/blob/master/docs/specs/nativesized.md).

I propose:

1. Ensuring that explicit conversion from all of F#'s primitive types is supported
2. Support arithmetic operators
3. Support generic numeric functions (`max`, `sqrt`, etc.)
4. 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:

1. Xamarin has introduced their own native types (`nint`, `nuint`, `nfloat`) in the `System` namespace which are usable but inelegant from F# since they rely heavily on implicit conversions and some constructors are private/internal.

2. 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.

```fsharp
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:

```csharp
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:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [ ] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.