Value restriction: The type 'bar' has been inferred to have generic type

后端 未结 1 496
无人及你
无人及你 2021-01-19 15:03

In the snippet below, I cannot see why I have to compose f and g the way function foo does and why it does not work the way function <

相关标签:
1条回答
  • 2021-01-19 15:54

    foo is a function, while bar is a value. Yes, it's a value of a function type, but still a value. There is a subtle difference there.

    The F# compiler can "see" that foo is a function, because it sees the fun -> right after let.

    Your bar, on the other hand, is a true value - a result obtained by invoking a different function (the operator >!). F# has a rule (known as "value restriction") saying (in everyday terms) that values (unlike functions) cannot have generic types, unless generic arguments are specified explicitly, thus effectively making it a "type function". (it's a bit more complicated than that, read the links below for the full picture)

    This is not specific to F#, though - other non-pure variants of ML have this as well. Here is a discussion of this rule for F#, and here's a discussion for SML.

    0 讨论(0)
提交回复
热议问题