Skip to content

Improve error reporting: Partially applied generic value vs function #1161

@isaacabraham

Description

@isaacabraham

What

The following code snippet returns an error message which could be improved upon:

let foo2 = Seq.map((*) 2) >> Seq.filter(fun x -> x < 10) // not ok
error FS0030: Value restriction. The value 'foo2' has been inferred to have generic type
    val foo2 : ('_a -> seq<int>) when '_a :> seq<int>    
Either make the arguments to 'foo2' explicit or, if you do not intend for it to be generic, add a type annotation.

Why

This error is confusing in several ways: -

  1. Explicitly providing the argument and moving from >> to |> removes the error e.g.
let foo a = a |> Seq.map((*) 2) |> Seq.filter(fun x -> x < 10) // ok
  1. It is unfortunate that the compiler has correctly determined that seq<int> would be acceptable yet cannot use it.
  2. Whilst the error message gives a couple of solutions, an example would help for both, or a URL to read up more on it. Also, the error message doesn't include a "why" this occurs. Granted, this may be too complex to do in this error message in this case, but I think that we should explore it nonetheless.

How

Open to suggestions here! Based on discussions with @swlaschin, @colinbull and @mavnn there are a few options. Let's start with this text: -

A let binding without a parameter creates a simple value and not a function. As this code is generic it has caused a value restriction. Consider adding a type annotation (or explicitly adding a parameter) to make this a function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature ImprovementTheme-Simple-F#A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions