dotnet / dotnet/fsharp

Inference error shadows additional error

Open
#12,958 0 comments 0 reactions 0 assignees View on GitHub
Area-Diagnostics Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Consider the following code:

```fsharp
let greyscaleGradient minimum maximum numberOfItems : int seq =
if numberOfItems > 1 then
let step = (maximum - minimum) / (numberOfItems - 1)
let calculatedValues = seq {for n in 0..(numberOfItems-2) -> n * step}
Seq.append calculatedValues (seq {maximum})
else
seq {minimum}
```

This is a compile error because `seq{maximum}` is inferred causes `maximum` to be of type `unit`, but we're expecting an `int seq`.

However, this also hides an error on this line:

```fsharp
let step = (maximum - minimum) / (numberOfItems - 1)
```

So you might be misled into thinking that the issue is somewhere else, since the compiler appears to be okay with that line. Only when you over over it will you be led into more confusion:

image

Instead, if the error were not "shadowed", it might lead you to placing a `yield` in the first `seq` expression instead, which fixes the compile error.

I suppose it's a separate problem that `seq{maximum}` changes `maximum` from an `int` into a `unit` even though it's clearly being used to subtract from 2 lines earlier.

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.