dotnet / dotnet/fsharp

Improve Error Reporting: help with ambiguity between subtraction and negation

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

Description

### Problem / Potential Improvement

F# treats:
`2-1`(no spaces)
and
`2 - 1`(spaces surround minus sign from both sides)
the same, but
`2 -1` (space before minus sign, but not after).
is treated differently.
This can lead to a very easy to make, but hard to get right error messages for newcomers to the language.
(Source: I was/still am this newcomer)

### Code examples

Example 1:
```
let minusOne x = x -1
let twoPlusThreeMinusOne = 2 + minusOne 3
```

Would lead to:
_error FS0001: This expression was expected to have type 'int -> 'a' but here has type 'int'_

Example 2:
```
let getListWithoutFirstAndLastElement list =
let l = List.length list
list[1 .. l -2]
```
Would lead to:
_error FS0003: This value is not a function and cannot be applied._

### Why

For newcomers to the language, the difference between -1 and - 1 is very odd.
I know, because just recently I was that newcomer and this is the error that was the weirdest for me to debug.

### How to improve
I assume treating "-1" as "- 1" in those instances is out of the question.
So the best thing I can think of right now for Example 2 with error like this is explanation that -1 is treated as a negation not as subtraction.

_error FS0003: This value is not a function and cannot be applied.
warning: list[1 .. List.length list -1]
-1 is a negation not subtract operation. To get subtraction use „ - „_

I don’t have a good idea how to fix example 1.

Related: #1103

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.