为什么 F# 计算表达式需要构建器对象(而不是类)? [英] Why do F# computation expressions require a builder object (rather than a class)?

查看:17
本文介绍了为什么 F# 计算表达式需要构建器对象(而不是类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F# 计算表达式的语法如下:

F# computation expressions have the syntax:

ident { cexpr }

其中 ident 是构建器对象(此语法取自 Don Syme 2007 年的博客文章).

Where ident is the builder object (this syntax is taken from Don Syme's 2007 blog entry).

在我见过的所有示例中,构建器对象都是单例实例,并且启动时是无状态的.Don 给出了定义一个名为 attempt 的构建器对象的示例:

In all the examples I've seen, builder objects are singleton instances, and stateless to boot. Don gives the example of defining a builder object called attempt:

let attempt = new AttemptBuilder()

我的问题: 为什么 F# 不直接在计算表达式中使用 AttemptBuilder 类?当然,这种表示法可以像实例方法调用一样轻松地对静态方法调用进行去糖化.

My question: Why doesn't F# just use the AttemptBuilder class directly in computation expressions? Surely the notation could be de-sugared to static method calls just as easily as instance method calls.

使用实例值意味着理论上可以实例化同一类的多个构建器对象,大概以某种方式参数化,或者甚至(天堂禁止)具有可变内部状态.但我无法想象这会有什么用处.

Using an instance value means that one could in theory instantiate multiple builder objects of the same class, presumably parameterised in some way, or even (heaven forbid) with mutable internal state. But I can't imagine how that would ever be useful.

更新:我上面引用的语法建议构建器必须显示为单个标识符,这是一种误导,可能反映了该语言的早期版本.最新的 F# 2.0 语言规范将语法定义为:

Update: The syntax I quoted above suggests the builder must appear as a single identifier, which is misleading and probably reflects an earlier version of the language. The most recent F# 2.0 Language Specification defines the syntax as:

expr { comp-or-range-expr }

这清楚地表明任何表达式(计算结果为构建器对象)都可以用作构造的第一个元素.

which makes it clear that any expression (that evaluates to a builder object) can be used as the first element of the construct.

推荐答案

你的假设是正确的;构建器实例可以被参数化,并且参数可以随后在整个计算过程中使用.

Your assumption is correct; a builder instance can be parameterized, and parameters can be subsequently used throughout the computation.

我使用这种模式为特定计算构建数学证明树.每个结论都是一个问题名称、一个计算结果和一个潜在结论的N-tree的三元组(引理).

I use this pattern for building a tree of mathematical proof to a certain computation. Each conclusion is a triple of a problem name, a computation result, and a N-tree of underlying conclusions (lemmas).

让我提供一个小例子,删除一个证明树,但保留一个问题名称.我们称之为注释,因为它看起来更合适.

Let me provide with a small example, removing a proof tree, but retaining a problem name. Let's call it annotation as it seems more suitable.

type AnnotationBuilder(name: string) =
    // Just ignore an original annotation upon binding
    member this.Bind<'T> (x, f) = x |> snd |> f
    member this.Return(a) = name, a

let annotated name = new AnnotationBuilder(name)

// Use
let ultimateAnswer = annotated "Ultimate Question of Life, the Universe, and Everything" {
    return 42
}
let result = annotated "My Favorite number" {
    // a long computation goes here
    // and you don't need to carry the annotation throughout the entire computation
    let! x = ultimateAnswer
    return x*10
}

这篇关于为什么 F# 计算表达式需要构建器对象(而不是类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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