Skip to content

Conversation

@Happypig375
Copy link
Member

@Happypig375 Happypig375 commented Aug 27, 2023

Improve value restriction error message, relates to #1103, fixes #1161

Implemented message for value restriction error

For function values

Value restriction: The value '{0}' has an inferred generic function type
    {1} 
However, values cannot have generic type variables like '_a in "let f: '_a". You should define '{2}' as a function instead by doing one of the following:
- Add an explicit parameter that is applied instead of using a partial application like "let f param"
- Add a unit parameter like "let f()"
- Write explicit type parameters like "let f<'a>",
or if you do not intend for it to be generic, either:
- Add an explicit type annotation like "let f : obj -> obj"
- Apply arguments of non-generic types to the function value in later code for type inference like "do f()".
This error is because a let binding without parameters defines a value, not a function. Values cannot be generic because reading a value is assumed to result in the same everywhere but generic type parameters may invalidate this assumption by enabling type-dependent results.

For non-function values

Value restriction: The value '{0}' has an inferred generic type
    {1}
However, values cannot have generic type variables like '_a in "let x: '_a". You can do one of the following:
- Define it as a simple data term like an integer literal, a string literal or a union case like "let x = 1"
- Add an explicit type annotation like "let x : int"
- Use the value as a non-generic type in later code for type inference like "do x"
or if you still want type-dependent results, you can define '{2}' as a function instead by doing either:
- Add a unit parameter like "let x()"
- Write explicit type parameters like "let x<'a>".
This error is because a let binding without parameters defines a value, not a function. Values cannot be generic because reading a value is assumed to result in the same everywhere but generic type parameters may invalidate this assumption by enabling type-dependent results.

@Happypig375 Happypig375 requested a review from a team as a code owner August 27, 2023 18:37
@Happypig375 Happypig375 marked this pull request as draft August 27, 2023 18:43
@Happypig375 Happypig375 marked this pull request as ready for review September 1, 2023 08:36
@Happypig375
Copy link
Member Author

Ready for review

Copy link
Member

@vzarytovskii vzarytovskii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lgtm, @dotnet/fsharp-team-msft

Copy link
Contributor

@psfinaki psfinaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@vzarytovskii vzarytovskii enabled auto-merge (squash) September 18, 2023 15:51
@psfinaki
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@psfinaki
Copy link
Contributor

@Happypig375 looks like this somehow intertwined with the latest parens removal code fix. Would you like to look at it? I can jump on that in the upcoming days if you want to want to bother. Should be something simple though hopefully.

@psfinaki
Copy link
Contributor

Sorry false alarm, updating the branch.

@edgarfgp
Copy link
Contributor

Please do not let this PR stale. the new error message is much better.

@psfinaki
Copy link
Contributor

Thanks for reminding, Edgar.

@Happypig375 will you have a moment to resolve the conflicts and address the remaining notes? If not, I can help with that this week.

@Happypig375
Copy link
Member Author

I can revisit this next week but @psfinaki if you want you can go ahead with this PR

@psfinaki
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@psfinaki
Copy link
Contributor

@Happypig375 so I'll be merging this - thanks for the PR!

@psfinaki psfinaki merged commit 311360d into dotnet:main Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Improve error reporting: Partially applied generic value vs function

4 participants