为什么F#的类型推断如此反复无常? [英] Why is F#'s type inference so fickle?

查看:79
本文介绍了为什么F#的类型推断如此反复无常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#编译器似乎以(相当)严格的从上到下,从左到右的方式执行类型推断.这意味着您必须做一些事情,例如将所有定义放到使用之前,文件编译的顺序很重要,并且您往往需要重新排列内容(通过|>或拥有的内容),以避免使用显式的类型注释.

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 their use, order of file compilation is significant, and you tend to need to rearrange stuff (via |> or what have you) to avoid having explicit type annotations.

使它变得更加灵活有多难,并且计划在将来的F#版本中使用它吗?显然,这是可以做到的,因为Haskell(例如)在推理能力相当强的情况下没有这种限制.导致这种情况的F#的设计或意识形态与生俱来有何不同?

How hard is it to make this more flexible, and is that planned for a future version of F#? Obviously it can be done, since Haskell (for example) has no such limitations with equally powerful inference. Is there anything inherently different about the design or ideology of F# that is causing this?

推荐答案

关于"Haskell同样强大的推论",我认为Haskell不必处理

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

  • OO风格的动态子类型(类型类可以做一些类似的事情,但类型类更易于键入/推断)
  • 方法重载(类型类可以做一些类似的事情,但类型类更易于键入/推断)

也就是说,我认为F#必须处理Haskell不需要的一些困难的东西. (几乎可以肯定,Haskell必须处理F#不会处理的一些难题.)

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.)

正如其他答案所提到的那样,大多数主要的.NET语言都将Visual Studio工具作为主要的语言设计影响力(请参见例如LINQ如何具有"from ... select"而不是"SQL-y" select. .. from",其动机是从程序前缀获取智能感知. Intellisense,错误信息和错误消息的可理解性都是影响F#设计的工具因素.

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.

有可能做得更好,推断更多(不牺牲其他经验),但是我不认为这是我们未来版本语言的重中之重. (Haskellers可能认为F#类型推断有些微弱,但可能比认为F#类型推断非常强的C#人数要多.:))

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.

这篇关于为什么F#的类型推断如此反复无常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆