Skip to content

F# Generic Constraints Table Excludes type-parameter #29000

@kroppt

Description

@kroppt

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions