F#和如何使用泛型

时间:2019-06-11 10:16:57

标签: f#

嗨,我有一个应返回通用值选项的函数,它看起来像:

let HandleHeaderConfirmation (client : TcpClient) (message : BaseFrame) (responseId : uint32, frameId: uint32) (isAcknowledgment : bool): Async<(Option<'TCommand> * TcpClient)> = 

    async{
        match isAcknowledgment with
            | false ->      
                let reason : NackRejectionReason = NackRejectionReason.InvalidCrc
                let nack : NackCommand = Confirmations.BuildNackResponse reason message (responseId, frameId)

                _logger.Info(sprintf "Sending header(CRC8) NACK reason %s" ((NackRejectionReason.InvalidCrc).ToString()))                                        
                _logger.Info(sprintf "Invalid header CRC8: %o raw header data %A" message.HeaderCrc message.RawHeaderData) 

                return (Some nack, client)
            | _ -> 
                _logger.Info(sprintf "Correct header(CRC8) message accepted")

                let ack : AckCommand = new AckCommand(responseId)
                return (Some ack, client)

    }

有关更多详细信息,Nack和Ack Commands继承自BaseResponseCommand,并且继承自基本框架。

我的问题是F#仅将'TCommand定义为NackCommand,如果我将其更改为

Async<(Option<BaseFrame> * TcpClient)>

它期望基本帧不是Nack或Ack Command。这样有可能吗?

1 个答案:

答案 0 :(得分:2)

将值传递到BaseFrame时,需要将其值上载到Some

option<BaseFrame>