我正在学习RX,并试图将C#中的一些代码移植到F#。 以下是C#示例使用定时器:F#RX使用定时器
Console.WriteLine("Current Time: " + DateTime.Now);
var source = Observable.Timer(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)).Timestamp();
using (source.Subscribe(x => Console.WriteLine("{0}: {1}", x.Value, x.Timestamp)))
{
Console.WriteLine("Press any key to unsubscribe");
Console.ReadKey();
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
以下是我的代码试图做同样的:
#light
open System
open System.Collections.Generic
open System.Linq
open System.Reactive
open System.Reactive.Linq
open System.Reactive.Subjects
printfn "Current Time: %A" DateTime.Now
let source = Observable.Timer(TimeSpan.FromSeconds(5.0), TimeSpan.FromSeconds(1.0)).Timestamp()
source.Subscribe(fun x -> printfn "%A %A" x.Value x.Timestamp) |> ignore
但我得到编译器错误:
错误1根据此计划点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这可能会使查找得到解决。
我不知道x.Value
和x.Timestamp
怎么样。顺便说一下,我也不知道如何在C#中使用F#重写。 请为我显示正确的代码。
嗨,Gene Belitski:非常感谢,您的代码有效!但是我很难知道x的类型,就像在C#代码中那样,类型也是缺失的。 – 2012-01-07 20:45:44
@John:由于涉及的类型的解释需要更多空间,而不是最大允许评论的空间,所以我将它作为我原始答案的附录。为了清楚起见,您可能希望使用[System.Reactive](http://msdn.microsoft.com/zh-cn/library/hh229356(v = VS.103).aspx)作为参考来跟踪这种类型转换链。 – 2012-01-07 21:32:46