-
Notifications
You must be signed in to change notification settings - Fork 6k
Closed
Labels
Description
The main part of the syntax is listed as:
type-parameter-list when constraint1 [ and constraint2]
The following table is listed on the page for the constraints:
Constraint | Syntax |
---|---|
Type Constraint | type-parameter :> type |
Null Constraint | type-parameter : null |
Explicit Member Constraint | [(]type-parameter [or ... or type-parameter)] : (member-signature) |
Constructor Constraint | type-parameter : ( new : unit -> 'a ) |
Value Type Constraint | : struct |
Reference Type Constraint | : not struct |
Enumeration Type Constraint | : enum<underlying-type> |
Delegate Constraint | : delegate<tuple-parameter-type, return-type> |
Comparison Constraint | : comparison |
Equality Constraint | : equality |
Unmanaged Constraint | : unmanaged |
Several of these exclude type-parameter. However, when I tested it with .NET 6, I found that it was actually required.
Consider the Comparison Constraint constraint.
❌ The following code does not compile:
let function1<'a when : comparison> = ()
✔️ The following code does compile:
let function1<'a when 'a: comparison> = ()
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 10e577e7-ce52-aa95-c2cc-b7c542af6f1b
- Version Independent ID: 7a1e9fb2-e1fb-04f2-2db5-b74b00d9c620
- Content: Constraints - F#
- Content Source: docs/fsharp/language-reference/generics/constraints.md
- Product: dotnet-fsharp
- GitHub Login: @cartermp
- Microsoft Alias: dotnetcontent