【发布时间】:2015-07-15 16:16:26
【问题描述】:
给定 F# 中的以下类型
type Message = string * AsyncReplyChannel<SymbolicExpression>
我该如何构建它? F# for fun and profit 的好东西,但我找不到如何构造新的“代数数据类型”Message。
这是我想要做的:
member x.Evaluate(expression: string, ?timeout) : SymbolicExpression =
agent.PostAndReply(fun c -> Message (expression,c), ?timeout=timeout)
我正在尝试将MailboxProcessor 公开给 C#,并尝试使尽可能多的 F# 特定类型远离 C# 代码。因此,我试图只接受一个字符串并返回一个 SymbolicExpression 类型(来自 RDotNet 命名空间)。
更新
好的 - 这是完整的来源。
open RDotNet
type Message = string * AsyncReplyChannel<SymbolicExpression>
type RInterfaceAgent(dllpath:string, rhome:string) =
let engine =
RDotNet.REngine.SetEnvironmentVariables(dllpath,rhome)
RDotNet.REngine.GetInstance()
let agent = MailboxProcessor<Message>.Start(fun inbox ->
let rec messageLoop n = async {
let! (msg, channel) = inbox.Receive()
engine.Evaluate(msg) |> channel.Reply
do! messageLoop (n+1)
}
messageLoop 0
)
member x.Evaluate(ex: string, ?timeout) : SymbolicExpression =
agent.PostAndReply((fun c -> Message (ex, c)), ?timeout=timeout)
这部分的错误信息:(fun c -> Message (ex, c))是:
未定义值或构造函数消息
【问题讨论】:
-
嗨@ildjarn - 谢谢,但不起作用。该类型没有构造函数 - 这是我的问题。我正在寻找一种解决方案来添加它,或者不添加它,或者以防我做一些愚蠢的事情 - 做其他事情。
-
您的
Message是元组的别名,因此您可以自己传递元组或将类型Message更改为 DU,例如type Message = | Message of string * AsyncReplyChannel<SymbolicExpression> -
为什么你的错误信息显示
expression而不是ex? -
@Guvante 因为我重构了
-
@spike:您有一个名为
ex的参数,并且您在屏幕截图中传入了expression。