dotnet / dotnet/fsharp

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

Open
#14,338 3 comments 3 reactions 0 assignees View on GitHub
Area-Compiler-SRTP Needs-RFC Resolution-By Design
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

**Repro steps**
```fsharp
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:

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

**Known workarounds**

Add the constraint:

```fsharp
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

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.