fsharp / fsharp/fslang-suggestions

Constant expressions in patterns

Open
#1,408 9 comments 1 reaction 0 assignees View on GitHub
area: enums area: pattern-matching
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

**I propose we** have a way to embed arbitrary constant expressions in patterns, such as via the following syntax:
```fs
match x
| 1 -> () // a single literal, already allowed
| const (1 + 10 * 20) -> () // a constant expression
| const X -> () // defined literal identifier, not a variable
```
The `const` pattern has the same semantics as a simple constant pattern. The precedence should be the same as for `const` 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 as `1000 / 20`. Not all such constants are "magic values" to warrant giving them a name, for example flags enums:
```fs
open System.Reflection
open type TypeAttributes

let t = typeof
let flags = t.Attributes &&& (Abstract ||| Sealed ||| Interface)

match flags with
| Abstract -> printf "abstract class" // incorrect - interpreted as a variable name even with the `open type`
| Abstract ||| Sealed -> printf "static class" // incorrect - unexpected symbol
| Sealed -> printf "sealed class"
| Class -> printf "class"
| _ -> printf "other"
```
With a way to use an arbitrary expression, such a code could be written without significant restructuring as:
```fs
match flags with
| const Abstract -> printf "abstract class"
| const (Abstract ||| Sealed) -> printf "static class"
| const Sealed -> printf "sealed class"
| const Class -> printf "class"
| _ -> printf "other"
```

**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!)

* [x] This is not a question (e.g. like one you might ask on [StackOverflow](http://stackoverflow.com)) and I have searched StackOverflow for discussions of this issue
* [x] 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](https://github.com/dotnet/fsharp)
* [x] 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
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate

* [x] This is not a breaking change to the F# language design
* [x] 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 :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no files, tests, or implementation entry point; begin in the F# compiler and tooling repository referenced by the proposal and review related suggestion #1018. Done would require an agreed language design for const patterns, including precedence and constant-expression semantics, followed by implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.