gpt4 book ai didi

f# - 如何编写通用 F# 委托(delegate)声明?

转载 作者:行者123 更新时间:2023-12-04 23:41:55 26 4
gpt4 key购买 nike

那么,如何在 f# 中编写通用委托(delegate)声明?

我想得到这个 c# 声明的等价物:

public delegate Result<TOutput> Parser<TInput, TValue>(TInput input);

我一直无法找到有关如何实现这一点的文档,尽管必须编写通用委托(delegate)是很常见的。

任何想法或指示?

最佳答案

您可以按如下方式定义通用委托(delegate):

type Parser<'TInput, 'TOutput> = delegate of 'TInput -> Result<'TOutput>

在 F# 中,泛型参数以单引号开头(以区别于普通类型),因此需要 'TInput .

只是为了记录,我认为我不需要在 F# 中定义委托(delegate)。我猜它们对 C# 互操作仍然有用,但即便如此,我可能只是为 Func 定义一个类型别名。委托(delegate),或者(当你不打算从 C# 调用它时,只是一个普通的 F# 函数):
// Alias for a standard .NET delegate
type Parser<'TInput, 'TOutput> = System.Func<'TInput, Result<'TOutput>>

// Alias for a normal F# function
type Parser<'TInput, 'TOutput> = 'TInput -> Result<'TOutput>

关于f# - 如何编写通用 F# 委托(delegate)声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34866516/

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