繁体   English   中英

函数返回值中的F#类型错误

[英]F# type error in function return value

我正在尝试在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#会自动将函数Func<T>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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM