问题描述
通读 F#4.0规范,我在PDF的第79页上看到了以下内容:
Reading through the F# 4.0 spec, I saw the following on page 79 of the PDF:
这是在规范的详细说明(非常详细)的上下文中,该说明描述了如何解析计算表达式并将其转换为表达式的构建器对象上的一系列方法调用. Scott Wlaschin的计算表达式系列,我发现它对帮助我理解其余部分非常有用.计算表达式背后的概念,没有提到 Source
方法,也没有找到我可以找到的任何其他参考.(Google对此没有多大帮助,因为很多人都在谈论源代码,而对 Source
方法的任何引用都被埋藏了.)
This is in the context of the spec's detailed (VERY detailed) description of how computation expressions are parsed and turned into a series of method calls on the expression's builder object. Scott Wlaschin's Computation Expressions series, which I found to be invaluable in helping me understand the rest of the concepts behind computation expressions, doesn't mention a Source
method, nor does any other reference I've been able to find. (Google isn't much help with this one, as tons of people talk about source code and any references to Source
methods gets buried).
在有关计算表达式的MSDN页面. QueryBuilder类使用 Source
我有一个例子可以看,但是没有解释为什么在其他情况下这很有用.
I also don't see Source
documented anywhere in the MSDN page on computation expressions. The QueryBuilder class uses Source
, so I have one example I can look at, but there's no explanation of why this would be useful in other circumstances.
在什么情况下,您希望在自定义计算表达式生成器上使用 Source
方法?
Under what circumstances would you want to have a Source
method on a custom computation expression builder? What would be a scenario where the default ForEach
handling is inadequate to one's needs and a Source
method would be useful?
推荐答案
我对此没有任何内幕知识,但是这就是为什么我认为该方法存在的原因,基于内置的翻译和实现> QueryBuilder
.
I do not have any insider knowledge about this, but here is why I think the method exists, based on the translation and the implementation in built-in QueryBuilder
.
QueryBuilder
中的所有操作都使用 QuerySource<'R,'Q>
表示数据源,其中'R
是元素的类型而'Q
是一种数据源- IEnumerable
或 IQueryable
.
All the operations in QueryBuilder
represent the data source using QuerySource<'R, 'Q>
where 'R
is the type of elements and 'Q
is the kind of data source - either IEnumerable
or IQueryable
.
只有一种数据类型这一事实意味着它不需要为 IQueryable
和 IEnumerable
定义单独的重载-否则将需要这些重载,因为最后,运行
方法需要对 IEnumerable
和 IQueryable
做不同的事情.
The fact that there is just one data type means it does not need to define separate overloads for IQueryable
and IEnumerable
- which would be otherwise needed because the Run
method at the end needs to do different things for IEnumerable
and for IQueryable
.
因此, Source
方法使您可以将查询可以处理的任何输入转换为数据源的某些内部表示形式".相反, Run
方法将数据从此内部表示形式转换为普通值.(如果使用 QueryBuilder
,则您自己的代码中永远不会看到 QuerySource
类型.)
So, the Source
method lets you transform whatever inputs the query can work on into some "internal representation" of the data source. On the opposite end, the Run
method turns the data from this internal representation into ordinary value. (In case of QueryBuilder
, you never see the QuerySource
type in your own code.)
这篇关于为什么要在自定义计算表达式生成器中使用Builder.Source()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!