F# 帮助类型推断的方法? [英] F# Ways to help type inference?

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

问题描述

Expert F# 2.0 中,作者是 Don Syme、Adam Granicz 和 Antonio Cisternino,pg.44

In Expert F# 2.0 by Don Syme, Adam Granicz, and Antonio Cisternino, pg. 44

类型推断:使用 |> 运算符可以让类型信息从操作这些对象的函数的输入对象.F# 使用从类型推断收集的信息以解决某些语言构造,例如属性访问和方法重载.这依赖于在文本中从左到右传播的信息的一个程序.特别是,在位置右侧键入信息解析属性访问和重载时不考虑.

Type inference: Using the |> operator lets type information flow from input objects to the functions manipulating those objects. F# uses information collected from type inference to resolve some language constructs such as property accesses and method overloading. This relies on information being propagated left to right thorough the text of a program. In particular, type information to the right of a position isn't taken into account when resolving property access and overload.

很明显地使用 |> 可以帮助类型推断.

So clearly using |> can help type inference.

与往常一样,声明类型也很有帮助.

As always, declaring the type is also helpful.

是否有其他方法/策略可用于帮助 F# 类型推断?

Are there any other means/tactics that can be used to help F# type inference?

编辑

正如 RamonSnir 正确指出的那样,应该让类型推断做尽可能多的工作.所以仅仅因为你可以添加类型声明并不是人们应该做的.不要把这个问题或答案当作应该做的事情.我问这个问题是为了帮助更好地理解类型推断的细微差别,以及在类型推断需要帮助的情况下什么可能有帮助.因此,如果类型推断可以在没有帮助的情况下解决所有类型,那么不要提供任何帮助,但是当它这样做时,知道一些帮助它的方法会很好.

As RamonSnir correctly pointed out one is supposed to let type inference do as much work as possible. So adding type declarations just because you can is not what one should do. Do not take this question or answers as something that should be done. I asked the question to help better understand the nuance of type inference and what might help in those occasions when type inference needs help. So if type inference can resolve all of the types without help, then don't give it any, but when it does, it would be nice to know some ways to help it.

推荐答案

几点:

1) 更喜欢模块函数而不是属性和方法.

1) Prefer module functions to properties and methods.

List.map (fun x -> x.Length) ["hello"; "world"] // fails
List.map String.length ["hello"; "world"] // works

let mapFirst xss = Array.map (fun xs -> xs.[0]) xss // fails
let mapFirst xss = Array.map (fun xs -> Array.get xs 0) xss // works

2) 更喜欢没有重载的方法.例如,QuickLinq Helpers 定义非重载成员以避免LINQ 扩展方法中的一堆类型注解.

2) Prefer methods without overloading. For example, QuickLinq Helpers define non-overloaded members to avoid a bunch of type annotation in LINQ extension methods.

3) 利用任何可用信息为类型检查器提供一些提示.

3) Make use of any available information to give some hints to the type checker.

let makeStreamReader x = new System.IO.StreamReader(x) // fails
let makeStreamReader x = new System.IO.StreamReader(path=x) // works

最后一个例子取自一篇关于 F# 类型推断的优秀文章.

The last example is taken from an excellent write-up about F# type inference.

总而言之,您通常不需要帮助 F# 类型检查器.如果存在类型错误,上面链接中的摘要提供了一个很好的修复指南:

To conclude, you don't often need to help F# type checker. If there is a type error, the summary from the link above gives a good fixing guideline:

总而言之,如果编译器是抱怨缺少类型或信息不足的情况是:

So to summarize, the things that you can do if the compiler is complaining about missing types, or not enough information, are:

  • 在使用之前定义内容(这包括确保以正确的顺序编译文件)
  • 将具有已知类型"的事物放在具有未知类型"的事物之前.特别是,您可以重新排序管道和类似的链式函数,以便类型化的对象在前.
  • 根据需要进行注释.一个常见的技巧是添加注释直到一切正常,然后将它们一一删除,直到你有所需的最低限度.如果可能,请尽量避免注释.不仅是不是不美观,反而让代码更脆弱.如果没有明确的类型,更改类型会容易得多对它们的依赖.

这篇关于F# 帮助类型推断的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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