开发者

F# Implementing Interface, Multiple Parameters, getting error this override takes a different number of

开发者 https://www.devze.com 2023-01-24 07:18 出处:网络
I have defined the following interface in F# [<ServiceContract>] type ICarRentalService = [<OperationContract>]

I have defined the following interface in F#

[<ServiceContract>]
type ICarRentalService =
    [<OperationContract>]
    abstract member CalculatePrice: pickupDate:DateTime -> returnDate:DateTime -> pickupLocation:string -> vehiclePreference:string -> float

then I tried t开发者_开发问答o implement it like this:

type CarRentalService() =
    interface ICarRentalService with
        override this.CalculatePrice(pickupDate:DateTime, returnDate:DateTime, pickupLocation:string, vehiclePreference:string) =
            5.5

When compiling I get the following compile error:

This override takes a different number of arguments to the corresponding abstract member

I'm now looking at the thing and fiddling around for an hour, what do I do wrong?


Method in your interface is declared in curried form and your implementation is tupled: if briefly: method in interface is function that accepts one argument and returns another function with remaining arguments. In opposite implementation accepts all arguments in one piece (packed in tuple)

open System
type ICarRentalService =
    abstract member CalculatePrice: pickupDate:DateTime -> returnDate:DateTime -> pickupLocation:string -> vehiclePreference:string -> float

let x : ICarRentalService = failwith "not implemented"
let a = x.CalculatePrice // DateTime -> DateTime -> string -> string -> float
let y = a (DateTime.Now) // DateTime -> string -> string -> float (first argument is bound)

To fix the code you need either to make implementation curried or declaration - tupled. Curried version will not work with WCF so consider using tupled version

type ICarRentalService =
    abstract member CalculatePrice: pickupDate:DateTime * returnDate:DateTime * pickupLocation:string * vehiclePreference:string -> float

type CarRentalService() =
    interface ICarRentalService with
        override this.CalculatePrice(pickupDate:DateTime, returnDate:DateTime, pickupLocation:string, vehiclePreference:string) =
            5.5
0

精彩评论

暂无评论...
验证码 换一张
取 消