F# 实现接口,多个参数,出现错误此覆盖需要不同数量的

发布于 2024-10-01 07:32:06 字数 711 浏览 1 评论 0原文

我在 F# 中定义了以下接口,

[<ServiceContract>]
type ICarRentalService =
    [<OperationContract>]
    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

编译时出现以下编译错误:

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

我现在正在查看这个东西并摆弄了一个小时,我做错了什么?

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 to 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-10-08 07:32:06

接口中的方法以柯里化形式声明,并且您的实现是元组的:
简而言之:接口中的方法是接受一个参数并返回另一个带有剩余参数的函数的函数。在相反的实现中,接受一个片段中的所有参数(打包在元组中)

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)

要修复代码,您需要使实现柯里化或声明 - 元组。柯里化版本不适用于 WCF,因此请考虑使用元组版本

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文