Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4208

F# and how to use generics

Hi i have a function that should return a generic value option and it looks like:

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)

    }

And for more details Nack and Ack Commands inherits from BaseResponseCommand and that inherits from base frame.

My problem is F# defines 'TCommand as NackCommand only and if I change this to

Async<(Option<BaseFrame> * TcpClient)>

It expects base frame not Nack or Ack Command. So is such a thing possible??

Upvotes: 1

Views: 66

Answers (1)

Tarmil
Tarmil

Reputation: 11372

You need to upcast your values to BaseFrame when passing them to Some where you need to have an option<BaseFrame>:

return (Some (nack :> BaseFrame), client)

Upvotes: 2

Related Questions