Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal, nativeint and unativeint literals #847

Open
5 tasks done
Happypig375 opened this issue Mar 10, 2020 · 5 comments
Open
5 tasks done

Decimal, nativeint and unativeint literals #847

Happypig375 opened this issue Mar 10, 2020 · 5 comments

Comments

@Happypig375
Copy link
Contributor

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.
Together with decimal constants, C# 9 can now define three types of literals that F# cannot. To interface with them, we must be able to interpret and define decimal, nativeint and unativeint literals.

let [<Literal>] Decimal = 3m
let [<Literal>] NativeInt = 3n
let [<Literal>] 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:
#562

Affidavit (please submit!)

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 or my company would be willing to help implement and/or test this
@dsyme
Copy link
Collaborator

dsyme commented May 28, 2020

Marked this approved-in-principle

@yatli
Copy link

yatli commented May 29, 2020

One minor suggestion, the name "unativeint" looks a bit like "un-native int" to me. Consider "NativeUInt"?

@Happypig375
Copy link
Contributor Author

Happypig375 commented May 29, 2020

No. unativeint is a name that already exists in FSharp.Core.

@esbenbjerre
Copy link

esbenbjerre commented Sep 18, 2024

What is expected in terms of codegen for a decimal literal?
Existing types supported by the literal attribute are generated as static literals directly in the IL, e.g. for

[<Literal>]
let lit01 = 35

we generate

.field public static literal int32 lit01 = int32(0x00000023)

But decimal requires the initialization of a System.Decimal object.

@vzarytovskii
Copy link

vzarytovskii commented Sep 18, 2024

But decimal requires the initialization of a System.Decimal object.

Yes, in case of const, it should be done via attribute and initonly value:

 .field public static initonly valuetype [System.Runtime]System.Decimal dconst
 // Value:
 .custom instance void [System.Runtime]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8, uint8, uint32, uint32, uint32) = (01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00)

Additionally, there's a ldsfld+stsfld is generated in the cctor:

.method private hidebysig specialname rtspecialname static void .cctor () cil managed 
{
    IL_0000: ldsfld valuetype [System.Runtime]System.Decimal [System.Runtime]System.Decimal::One
    IL_0005: stsfld valuetype [System.Runtime]System.Decimal Constants::dconst
    IL_000a: ret
}

for public const decimal dconst = 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants