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

查看:19
本文介绍了为什么 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-yselect"... 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#ers 多.:)

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天全站免登陆