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

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

问题描述

Don Syme,Adam Granicz和Antonio Cisternino,第专家F#2.0 中. 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:

  • 在使用之前定义内容(包括确保文件以正确的顺序编译)
  • 将具有已知类型"的事物放置在具有未知类型"的事物之前.特别是,您可以对管道进行重新排序 以及类似的链接函数,以便类型化的对象排在第一位.
  • 根据需要注释.一种常见的技巧是添加注释,直到一切正常,然后将它们一一拿掉,直到您拥有 最低要求.如果可能,请尽量避免添加注释.不仅 它在美学上不是令人满意的,但是会使代码更脆弱. 如果没有显式的更改类型会容易得多 对它们的依赖.
  • Define things before they are used (this includes making sure the files are compiled in the right order)
  • Put the things that have "known types" earlier than things that have "unknown types". In particular, you might be able reorder pipes and similar chained functions so that the typed objects come first.
  • Annotate as needed. One common trick is to add annotations until everything works, and then take them away one by one until you have the minimum needed. Do try to avoid annotating if possible. Not only is it not aesthetically pleasing, but it makes the code more brittle. It is a lot easier to change types if there are no explicit dependencies on them.

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

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