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

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

问题描述

F#计算表达式的语法为:

F# computation expressions have the syntax:

ident { cexpr }

其中ident是构建器对象(此语法来自

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树(引理).

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).

让我提供一个小例子,删除一个证明树,但保留一个问题名称.我们将其称为 annotation (注释),因为它似乎更合适.

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#计算表达式需要一个builder对象(而不是一个类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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