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

Allow constant values on Discriminated Unions #558

Closed
3 of 6 tasks
pedromsantos opened this issue Apr 8, 2017 · 0 comments
Closed
3 of 6 tasks

Allow constant values on Discriminated Unions #558

pedromsantos opened this issue Apr 8, 2017 · 0 comments

Comments

@pedromsantos
Copy link

pedromsantos commented Apr 8, 2017

Allow constant values on Discriminated Unions

I propose we allow discriminated unions to have constant values defined on the type

The existing way of approaching this problem in F# is creating a pattern match function to associate values with DU

Pros and Cons

The advantages of making this adjustment to F# are simplicity in design, specially in programs that make heavy usage of constant data.

Example using the famous Java planets sample (http://snipplr.com/view/42422/the-planet-enum-example/):

Using a tuple to associate constant values:

type Planets =
| MERCURY of (3.303e+23, 2.4397e6)
| VENUS of (4.869e+24, 6.0518e6)
| EARTH of (5.976e+24, 6.37814e6)
| MARS of (6.421e+23, 3.3972e6)
| JUPITER of (1.9e+27, 7.1492e7)
| SATURN of (5.688e+26, 6.0268e7)
| URANUS of (8.686e+25, 2.5559e7)
| NEPTUNE of (1.024e+26, 2.4746e7)

Using an anonymous record to associate constant values (if we get anonymous records in F#):

type Planets =
| MERCURY of {Mass=3.303e+23; Radius=2.4397e6)
| VENUS of {Mass=4.869e+24; Radius=6.0518e6),
| EARTH of {Mass=5.976e+24; Radius=6.37814e6),
| MARS of {Mass=6.421e+23; Radius=3.3972e6),
| JUPITER of {Mass=1.9e+27; Radius=7.1492e7),
| SATURN of {Mass=5.688e+26; Radius=6.0268e7),
| URANUS of {Mass=8.686e+25; Radius=2.5559e7),
| NEPTUNE of {Mass=1.024e+26; Radius=2.4746e7);

The disadvantages of making this adjustment to F# are apart form the work involved, I honestly don't know :)

Extra information

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

Related suggestions: (put links to reated suggestions here)

Affadavit (must be submitted)

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 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
@jbeeko jbeeko mentioned this issue Apr 5, 2018
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant