gpt4 book ai didi

f# - 需要帮助将带有泛型的接口(interface)从 C# 转换为 F#

转载 作者:行者123 更新时间:2023-12-02 07:28:19 24 4
gpt4 key购买 nike

我有这个 C# 代码

public interface IDinnerRepository : IRepository<Dinner>
{
IQueryable<Dinner> FindByLocation(float latitude, float longitude);
IQueryable<Dinner> FindUpcomingDinners();
IQueryable<Dinner> FindDinnersByText(string q);
void DeleteRsvp(RSVP rsvp);
}

public interface IRepository<T>
{
IQueryable<T> All { get; }
IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
T Find(int id);
void InsertOrUpdate(T dinner);
void Delete(int id);
void SubmitChanges();
}

我想将其翻译成 F#。如何制作泛型接口(interface)? F# 接口(interface)的 MSDN 示例实际上并没有等效项。据我所知。

最佳答案

实际上有两种定义通用接口(interface)的方法,但几乎所有 F# 代码都使用您在现有答案中提到的样式。另一个选项仅用于某些标准 F# 类型,例如 'T option'T list:

type IRepository<'T> = // The standard way of doing things
type 'T IRepository = // Used rarely for some standard F# types

至于你的示例接口(interface)中出现的其他成员类型,其中一些其实有点意思,所以这里有一个比较完整的翻译(仅供以后引用!)

// We inherit from this type later, so I moved it to an earlier location in the file
type IRepository<'T> =
// Read-only properties are easier to define in F#
abstract All : IQueryable<'T>
// Annotating parameter with the 'params' attribute and also specifying param name
abstract AllIncluding
: [<ParamArray>] includeProperties:Expression<Func<'T, obj>>[] -> IQueryable<'T>


type IDinnerRepository =
// Inherit from another interface
inherit IRepository<Dinner>

// And add a bunch of other members (here I did not specify parameter names)
abstract FindUpcomingDinners : unit -> IQueryable<Dinner>
abstract FindDinnersByText : string -> IQueryable<Dinner>

关于f# - 需要帮助将带有泛型的接口(interface)从 C# 转换为 F#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com