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

Hexadecimal, octal and binary custom numeric literals #754

Open
5 tasks done
Happypig375 opened this issue Jun 14, 2019 · 8 comments
Open
5 tasks done

Hexadecimal, octal and binary custom numeric literals #754

Happypig375 opened this issue Jun 14, 2019 · 8 comments

Comments

@Happypig375
Copy link
Contributor

Hexadecimal, octal and binary custom numeric literals

I propose we allow first-class support of colors.

module NumericLiteralR = ... // Implementation unchanged from before
let x : Color = 0xff00ff00R // Hexadecimal color literal!

The existing way of approaching this problem in F# is either through functions taking non-decimal strings or converting from integers.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Consistency - custom numeric literals should have the same support with built-in ones
  2. Ease of use - enabling BigIntegers from their binary form
  3. Support for more DSLs - e.g. Colors

The disadvantage of making this adjustment to F# is an implementation in the F# compiler converting hexadecimal, octal and binary strings to decimal strings to call FromString would probably be required.

Extra information

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

Related suggestions: (put links to related suggestions here)

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
@yatli
Copy link

yatli commented Jun 21, 2019

I don't like this.. Because not all platforms support Color, and even for those do, a nuget package will be implicitly depend on.
The most similar thing that comes to my mind is the ValueTuple and C# combination, but at least ValueTuple supports down to netstandard1.0.

@Happypig375
Copy link
Contributor Author

Hexadecimal, octal and binary custom numeric literals

I'm talking about custom numeric literals, which are user-defined. Defining colors is one of the use cases if the mechanism is extended to support binary, octal and hexadecimal literals.

@yatli
Copy link

yatli commented Jun 22, 2019

That makes a lot more sense. I was drawn completely by Color :p
The C++ custom literal, FYI: https://en.cppreference.com/w/cpp/language/user_literal
Any ideas what operator shall we use (or attributes)?

@Happypig375
Copy link
Contributor Author

Using the current way, of course!

module NumericLiteralR =
    //let FromZero() = System.Drawing.Color.FromArgb(0)
    //let FromOne() = ...
    let FromInt32 n = System.Drawing.Color.FromArgb(n)
    //let FromInt64 n = ...
    //let FromString s = ...

let expected = System.Drawing.Color.FromArgb(123)
let value = 123R
expected = value
module NumericLiteralR = begin
  val FromInt32 : n:int -> System.Drawing.Color
end
val expected : System.Drawing.Color = Color [A=0, R=0, G=0, B=123]
val value : System.Drawing.Color = Color [A=0, R=0, G=0, B=123]
val it : bool = true

The problem is that hexadecimal is unsupported, but it is the standard representation.

let x = 0x123R
FS1156: This is not a valid numeric literal. Valid numeric literals include 1, 0x1, 0b0001 (int), 1u (uint32), 1L (int64), 1UL (uint64), 1s (int16), 1y (sbyte), 1uy (byte), 1.0 (float), 1.0f (float32), 1.0m (decimal), 1I (BigInteger).	

@yatli
Copy link

yatli commented Jun 22, 2019

wow, I didn't know that it could be done like this!
I've searched for relevant blog posts -- it seems that the suffix is limited to a set?
That'll make it less useful...

@Happypig375
Copy link
Contributor Author

Happypig375 commented Jun 22, 2019

Yes, Q, R, Z, I, N and G.
Q is used in Math.NET Symbolics to represent an algebraic expression.
R and Z are not used in any popular open source library on GitHub.
I is a built-in one, that allows BigInteger literals to be written.
N is used in Math.NET Numerics to represent a BigRational literal.
G is used in FSharpPlus to represent a literal with inferred type.

To be honest the use of custom numeric literals don't seem widespread enough. They being limited to only integral types may be a possible reason. We should expand support for these, e.g. hexadecimal representation.

@voronoipotato
Copy link

I wonder if type providers are a good way to make "literals". Essentially being a passthrough but with a transformation and compile time check

@Happypig375 Happypig375 changed the title 0xff00ff00R // Green Hexadecimal, octal and binary custom numeric literals Dec 15, 2019
@dsyme
Copy link
Collaborator

dsyme commented Jun 16, 2022

I'm ok with this - it should be possible to use hex, octal and binary custom literals with the allowable suffixes

@Happypig375 Could you update the examples to make it more obviouus that this is not about "Color", e.g. make the sample a full proposed example.

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

4 participants