gpt4 book ai didi

f# - 具有 F# 函数类型的扩展方法

转载 作者:行者123 更新时间:2023-12-04 18:07:34 24 4
gpt4 key购买 nike

关于类型扩展的 MSDN 文档指出“在 F# 3.1 之前,F# 编译器不支持使用带有泛型类型变量、数组类型、元组类型或 F# 函数类型作为“this”的 C# 风格的扩展方法。范围。” ( http://msdn.microsoft.com/en-us/library/dd233211.aspx )
如何在 F# 函数类型上使用类型扩展?在什么情况下这样的功能会有用?

最佳答案

您可以这样做:

[<Extension>]
type FunctionExtension() =
[<Extension>]
static member inline Twice(f: 'a -> 'a, x: 'a) = f (f x)

// Example use
let increment x = x + 1
let y = increment.Twice 5 // val y : int = 7

现在对于“在什么情况下这样的功能会有用?”,老实说,我不知道,我认为这样做可能是一个坏主意。在函数上调用方法感觉太像 JavaScript 了,在 F# 中根本不习惯。

关于f# - 具有 F# 函数类型的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23292379/

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