fsharp / fsharp/fslang-suggestions

Decimal, nativeint and unativeint literals

Open
#847 5 comments 12 reactions 0 assignees View on GitHub
approved-in-principle area: library
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Decimal, nativeint and unativeint literals

I propose we allow decimal, nativeint and unativeint literals.

The existing way of approaching this problem in F# is (none).

C# 9 now has [native-sized integers](https://github.com/dotnet/csharplang/blob/b75760a72480d4214bf68d3150a2ff649c80e95e/proposals/NativeInt.md).
[Together with decimal constants, C# 9 can now define three types of literals that F# cannot](https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQDlYEsBuECSAdjAD4ACATAAQDCVA3gLABQVbVAxgPaHIxUATCBywBbKABsqAEWFjJVALxUAzAG4W7Tjz5VCWYlXQH+ygGwbW7br36E4Jow8PKAnJYC+QA===). To interface with them, we must be able to interpret and define decimal, nativeint and unativeint literals.

```fs
let [] Decimal = 3m
let [] NativeInt = 3n
let [] UNativeInt = 3un
match 3n with NativeInt -> true | _ -> false // true
```

## Pros and Cons

The advantages of making this adjustment to F# are
1. Consistency between numeric types - all other numeric types can be defined as literals. Why not these three?
2. Increased interoperability with C#
3. More code reuse - more chances to refactor constant patterns in `match's into literals that can be changed easily

The disadvantages of making this adjustment to F# are none that I can think of.

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions:
https://github.com/fsharp/fslang-suggestions/issues/562

## Affidavit (please submit!)

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
* [x] 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
* [x] 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:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

Contributor guide

No contributing guide indexed for this repository

Research direction

No source files, tests, or entry points are named. Start with the proposed literal examples and related suggestion #562, then review the referenced C# native-sized integer proposal; done means an agreed implementation path covering 3m, 3n, 3un, Literal declarations, and match behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.