Skip to content

Improve Error Reporting: help with ambiguity between subtraction and negation #13014

@jasiozet

Description

@jasiozet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions