相互递归的F#函数,输入问题

时间:2012-02-28 18:14:08

标签: f#

此代码出现问题:

  let subscribe (nm : NamespaceManager) (subName : string) (desc : TopicDescription)  : Async<SubscriptionDescription> =
    let rec first_create () =
      async {
        let! exists = desc |> exists nm
        if exists then return! (then_create_subscription () : Async<SubscriptionDescription>)
        try
          let beginCreate = nm.BeginCreateTopic : string * AsyncCallback * obj -> IAsyncResult
          logger.DebugFormat("creating topic '{0}'", desc)
          let! tdesc = Async.FromBeginEnd(desc.Path, beginCreate, nm.EndCreateTopic)
          return! first_create ()
        with | :? MessagingEntityAlreadyExistsException -> return! then_create_subscription () }
    and then_create_subscription ()  : Async<SubscriptionDescription> =
      async {
        let beginCreate = nm.BeginCreateSubscription : string * string * AsyncCallback * obj -> IAsyncResult
        return! Async.FromBeginEnd(desc.Path, subName, beginCreate, nm.EndCreateSubscription) }
    first_create ()

在第5行,它强调then_create_subscription () : Async<SubscriptionDescription>说明:

  

类型不匹配。期待Async<unit>,但Async<SubscriptionDescription>类型'单位'与“SubscriptionDescription”类型不匹配

存在看起来像这样:

  let exists (nm : NamespaceManager ) (desc : PathBasedEntity) = 
    async { return! Async.FromBeginEnd(desc.Path, nm.BeginTopicExists, nm.EndTopicExists) }

我希望它创建主题,然后继续为其创建订阅。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

if之后需要有一个else分支,否则if是一个语句,而不是一个表达式。其他不是隐含的。