简体   繁体   中英

Mutually recursive F# functions, typing issue

Having trouble with this code:

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

On line 5, it underlines then_create_subscription () : Async<SubscriptionDescription> stating:

Type mismatch. Expecting a Async<unit> but given a Async<SubscriptionDescription> The type 'unit' does not match the type ' SubscriptionDescription '

Exists looks like this:

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

I want it to create the topic and then go on to create the subscription for it.

Any ideas?

There needs to be an else branch after if, otherwise the if is a statement, not an expression. The else is not implicit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM