Skip to content

Generic constraint comparison is required despite type being restricted to INumber (generic math .NET 7) #14338

Open
@gurustron

Description

@gurustron

Repro steps

let inline test<'T when 'T :> INumber<'T>> x:'T = 
    if x > 'T.Zero then x
    else 'T.Zero

Actual behavior

Compilation error:

Severity Code Description Project File Line Suppression State
Error FS0193 A type parameter is missing a constraint 'when 'T: comparison' FSharpTests Program.fs 37 Active

Expected behavior

Compiles like C# counterpart:

public T Tets<T>(T x) where T : INumber<T> => x > T.Zero ? x : T.Zero;

Known workarounds

Add the constraint:

let inline test<'T when 'T :> INumber<'T> and 'T:comparison> x:'T = 
    if x > 'T.Zero then x
    else 'T.Zero

Related information

  • Operating system Win 11
  • .NET 7
  • Visual Studio 17.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions