Why is F#'s type inference so fickle?

后端 未结 5 1513
失恋的感觉
失恋的感觉 2020-12-02 18:12

The F# compiler appears to perform type inference in a (fairly) strict top-to-bottom, left-to-right fashion. This means you must do things like put all definitions before th

5条回答
  •  情歌与酒
    2020-12-02 18:40

    Regarding "Haskell's equally powerful inference", I don't think Haskell has to deal with

    • OO-style dynamic subtyping (type classes can do some similar stuff, but type classes are easier to type/infer)
    • method overloading (type classes can do some similar stuff, but type classes are easier to type/infer)

    That is, I think F# has to deal with some hard stuff that Haskell does not. (Almost certainly, Haskell has to deal with some hard stuff that F# does not.)

    As is mentioned by other answers, most of the major .NET languages have the Visual Studio tooling as a major language design influence (see e.g. how LINQ has "from ... select" rather than the SQL-y "select... from", motivated by getting intellisense from a program prefix). Intellisense, error squiggles, and error-message comprehensibility are all tooling factors that inform the F# design.

    It may well be possible to do better and infer more (without sacrificing on other experiences), but I don't think it's among our high priorities for future versions of the language. (The Haskellers may see F# type inference as somewhat weak, but they are probably outnumbered by the C#ers who see F# type inference as very strong. :) )

    It might also be hard to extend the type inference in a non-breaking fashion; it is ok to change illegal programs into legal ones in a future version, but you have to be very careful to ensure previously-legal programs do not change semantics under new inference rules, and name resolution (an awful nightmare in every language) is likely to interact with type-inference-changes in surprising ways.

提交回复
热议问题