dotnet / dotnet/fsharp

`open type` does not bring enum cases into scope in patterns

Open
#17,630 9 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-Checking Area-Compiler-Checking-NameResolution Breaking-change Bug help wanted Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

I meant to open this years ago, but I guess I never did.

**Repro steps**

```fsharp
type E = A = 1 | B = 2

open type E

let f x =
match x with
| A -> () // This binds a new value `A` instead of matching the enum case E.A, which is otherwise in scope unqualified.
```

**Expected behavior**

Enum cases brought into scope with an `open type` declaration (or an `AutoOpen` on the enum definition) should be in scope in pattern matching as unqualified literals, just like union cases or active pattern cases.

E.g., if you change the enum in the example above to a union type, `A` _is_ brought into scope in something like this:

```fsharp
module M =
type E = A | B

open type M.E

let f x =
match x with
| A -> ()
| B -> ()
```

**Actual behavior**

Enum cases brought into scope with an `open type` declaration (or an `AutoOpen` on the enum definition) are not in scope in pattern matching as unqualified literals.

**Known workarounds**

N/A.

**Related information**

.NET 8, 9.

**Note**

I guess this probably can't be fixed, since there may now be existing code that relies on the current behavior — although it is quite possible that some of that existing code is wrong, because it was written with the understanding that `open type` would behave the same for enums as it does for unions, etc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.