dotnet / dotnet/fsharp

Unable to write SRTP constraints for method

Open
#5,620 5 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-SRTP Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

When writing the same `inline` method using statically resolved type parameters two times, first without specifying any type parameters and letting the compiler figure everything out, then by explicitly stating the type parameters and constraints, I can only get the first approach to work.

#### Repro steps

Place the following type into VS or VS Code.

```fsharp
type Summer () =

member inline _this.SumBy (items, selector) =
Seq.fold (fun acc item ->
acc + selector item
) LanguagePrimitives.GenericZero items

member inline _this.SumBy'<'T, ^U, ^V
when (^V or ^U) : (static member (+) : ^V * ^U -> ^V)
and ^V : (static member Zero : ^V)
> (items : 'T seq, selector : 'T -> ^U) : ^V =
Seq.fold (fun acc item ->
acc + selector item
) LanguagePrimitives.GenericZero items
```

The `acc` in the line `acc + selector item` of `SumBy'` will be underlined.

#### Expected behavior

Both `SumBy` and `SumBy'` should work.

#### Actual behavior
`SumBy'` shows the following:

![sumby](https://user-images.githubusercontent.com/15893812/45358695-8689d000-b5ca-11e8-8782-1f361322f735.PNG)

#### Known workarounds

Letting the compiler figure everything out. I hope I didn't miss anything obvious...

Or you can do what's described [in this comment](https://github.com/Microsoft/visualfsharp/issues/5620#issuecomment-420390719).

#### Related information

* Windows 10
* .NET 4.7
* VS 15.8.3 as well as VS Code 1.27.1 with Ionide 3.25.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.