fsharp / fsharp/fslang-suggestions
Make it optional to repeat the constraints in extensions
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
**I propose we** make it optional to repeat the constraints on type parameters when writing an extension on a generic type.
```fsharp
type Foo<'T when 'T : struct> = { x: 'T }
// OK
type Foo<'T when 'T : struct> with
member this.Y = 1
// Currently: error FS0957: One or more of the declared type parameters for this type extension
// have a missing or wrong type constraint not matching the original type constraints on 'Foo<_>'
// Proposal: make this valid
type Foo<'T> with
member this.Z = 1
```
**The existing way of approaching this problem in F# is** to repeat all constraints. However this is not always possible:
This is actually causing a regression in F# 9, as mentioned [here](https://hachyderm.io/@BentRs/113476256823511243). The problem is that .NET 9 introduces a new constraint `allows ref struct` which cannot (yet) be expressed in F#. So types that have this constraint currently cannot be extended. This is especially problematic because this constraint has been added to existing standard library types such as `IEnumerable`.
```fsharp
// F# 8: OK
// F# 9: error FS0957
type System.Collections.Generic.IEnumerable<'T> with
member _.Foo = ()
```
## Pros and Cons
**The advantages of making this adjustment to F# are**
* more conciseness when extending a type that has constraints.
* working around the `allows ref struct` regression mentioned above, and avoiding similar future regressions.
**The disadvantages of making this adjustment to F# are**
* constraints are less explicit I guess?
## Extra information
**Estimated cost (XS, S, M, L, XL, XXL):** S
**Related suggestions:** N/A
## Affidavit (please submit!)
Please tick these items by placing a cross in the box:
* [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
Please tick all that apply:
* [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
Start by reviewing the generic type-extension examples and the F# 9 regression involving `allows ref struct`, then examine the linked compiler and tooling repository for the language-design and implementation entry points. Done means extensions can omit repeated constraints while preserving existing constrained extensions and the shown `IEnumerable<'T>` case.
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