-
Notifications
You must be signed in to change notification settings - Fork 21
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
Constant expressions in patterns #1408
Comments
Related: #1018. |
Not a fan of a whole new language construct to be honest. If we really want to enable arbitrary constant expressions in the matching, we may instead think of a way of reducing/eliminating confusion and ambiguity (for both compiler and user) in cases of constants vs bindings of variables in cases instead. We should also keep in mind how will this affect pattern compilations and exhaustiveness checks, performance wise. |
I would also look at the enums without qualified enum name separately, this could be addressed separately without bringing in broader support for complex constant expressions. |
+1, this issue has been brought up multiple times already |
I would rather prefer having a general way that automatically covers enum cases too, i.e. That being said, there are other options too ‒ if type-qualified lookup of members in patterns is extended instead, I could (once dotnet/fsharp#18316 is fixed) write |
I feel like parameterized active patterns might already work for this? |
I propose we have a way to embed arbitrary constant expressions in patterns, such as via the following syntax:
The
const
pattern has the same semantics as a simple constant pattern. The precedence should be the same as forconst
used for static arguments, i.e. to require parentheses for complex expressions.The existing way of approaching this problem in F# is to define a literal value with the desired value (which must be placed in a module however, until #848 is resolved), or an active pattern that does the comparison (which is inefficient). In either case there is no way to in-line the expression.
Pros and Cons
The advantages of making this adjustment to F# are simplifying code that needs to match on constant values derived from multiple sources, such as
"[" + nameof X + "]"
, or has embedded meaning, such as1000<m> / 20<m/s>
. Not all such constants are "magic values" to warrant giving them a name, for example flags enums:With a way to use an arbitrary expression, such a code could be written without significant restructuring as:
The disadvantages of making this adjustment to F# are permitting a way to produce potentially "less clean" code. However, not having this feature also leads to significantly unclean code in some situations.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: #1018
Affidavit (please submit!)
This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
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
I have searched both open and closed suggestions on this site and believe this is not a duplicate
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
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: