.net - F#实现接口(interface),多个参数,出现错误,此覆盖需要不同数量的

标签 .net visual-studio-2010 f# .net-3.5 .net-4.0

我在 F# 中定义了以下接口(interface)

[<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

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

最佳答案

接口(interface)中的方法以 curried 形式声明,并且您的实现是元组的:
如果简单地说:接口(interface)中的方法是接受一个参数并返回另一个带有剩余参数的函数的函数。在相反的实现中接受所有参数(打包在元组中)

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)

要修复代码,您需要使实现柯里化(Currying)或声明 - 元组。 Curried 版本不适用于 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

关于.net - F#实现接口(interface),多个参数,出现错误,此覆盖需要不同数量的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4114435/

相关文章:

带参数的 SQL 插入适用于 MSAccess,但不适用于 SQL Server

c# - 将 RijndaelManaged 代码转换为 AesManaged 代码

c# - Serilog with Elasticsearch sink, indexformat 没有滚动日期

.net - 4.6.1 F# 应用程序抛出 FileNotFoundException : FSharp. 核心

F# 将字符串转换为 float

haskell - Haskell 程序员的计算表达式

c# - EF6 和分层架构

visual-studio-2010 - 使用 DiffMerge

vb.net - MsgBox 和 MessageBox.Show 有区别吗?

visual-studio - 无法将 SourceSafe 数据库添加为 Visual Studio 2010 源代码管理