I suggest we ...

Extend custom numeric types to support floating point literals

Currently you can define custom numeric types for integer values.
It would be nice to be able to handle floating point values too, e.g.

type complex = Complex of double * double
type imaginery = Imaginery of double
with
static member (+) (lhs:double,Imaginery(rhs)) = Complex(lhs,rhs)

module NumericLiteralI =
let FromZero () = Imaginery 0.0
let FromOne () = Imaginery 1.0
let FromInt32 (x) = Imaginery (double x)
let FromInt64 (x:int64) = Imaginery (double x)
let FromDouble (x:float) = Imaginery x // extension

let polar = 1.5 + 2.5I

13 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Phillip Trelford shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

3 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Abel commented  ·   ·  Flag as inappropriate

    @fsharporg-lang: The link to the suggestion "in favour of" is actually *this* suggestion. It seems that you mean to say you are actually going to implement this? Or did you mean to point to another suggestion?

  • Don Syme commented  ·   ·  Flag as inappropriate

    This is a nice suggestion - the Xamarin use-case below is of particular interest.

    I'm not marking it as "approved" as yet but I'd be interested in seeing a pull request ironing out any lurking design details.

  • trek42 commented  ·   ·  Flag as inappropriate

    This will be a very desired feature now that Xamarin unified API uses 'System.nfloat' to represent native float type and no longer uses either float or double. Because of this, I have to keep writing "nfloat <float-literal>" (e.g. "nfloat 2.0") for all float literals of nfloat type. It will greatly improve clarity and avoid verbosity if I could just say "2.0nf" where 'nf' denotes native float values.

F# Language

Feedback and Knowledge Base