gpt4 book ai didi

c# - F#函数返回值类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:46 24 4
gpt4 key购买 nike

我正在尝试在 F# 中实现以下代码片段:

    // Method style
void Callback (NSNotification notification)
{
Console.WriteLine ("Received a notification UIKeyboard", notification);
}

void Setup ()
{
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification, Callback);
}

我做了以下事情:

let toggleKeyboard(notification : NSNotification) = 
Console.WriteLine ("Received a notification UIKeyboard", notification)

NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, toggleKeyboard) |> ignore

这似乎是一个简单的实现,但是我得到了一个类型错误:

This expression was expected to have type 'Action<NSNotification>' but here has type ''a -> unit' (FS0001) (Dissertation)

我不太确定如何让我的方法返回 Action<NSNotification>类型。

最佳答案

有时 F# 会自动将函数转换为 Action<T>Func<T>类型,但您也可以将您的函数显式包装在 Action<T> 中像这样:

let foo x = printfn "%s" x
let action = System.Action<string>(foo)
action.Invoke("hey") // prints hey

或者在你的情况下:

...AddObserver(UIKeyboard.WillShowNotification, System.Action<NSNotification>(toggleKeyboard)) |> ignore

关于c# - F#函数返回值类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598457/

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