问题描述
F#(至少在Visual Studio 2012中)同时具有和)
另一方面 IObservable
能够删除处理程序。
IEvent
does not support removing of event handlers, so when you create a processing pipeline (combiningmap
,filter
and others) and then callRemoveHandler
on the resulting event, it leaves some handlers attached (yes, that's a leak and we wrote a more detailed paper about it)On the other handIObservable
is able to remove handlers.
作为上一点的结果,与状态组合器相比, IObservable
的行为有所不同。例如,当您使用 Event.scan
时,您可以将多个处理程序附加到生成的事件,并且它们将看到相同的状态。 IObservable
为每个附加处理程序创建一个新状态(除非您明确使用主题)。
As a result of the previous point, IObservable
behaves differently with respect to stateful combinators. For example, when you use Event.scan
, you can attach multiple handlers to the resulting event and they will see the same state. IObservable
creates a "new state" for every attached handler (unless you use subject explicitly).
在实际的F#编程中,这意味着:
In practical F# programming, this means:
-
你通常更喜欢
IObservable
如果您想要删除事件处理程序(使用RemoveHandler
或使用AwaitObservable
You should generally prefer
IObservable
if you want to be able to remove event handlers (usingRemoveHandler
or when usingAwaitObservable
in F# async workflows).
如果要声明事件(可从C#使用),则需要创建类型为 IEvent
,因此您需要使用事件
组合器。
If you want to declare events (usable from C#) then you need to create properties of type IEvent
and so you need to use Event
combinators.
如注释所述,F#模型受功能反应规划(FRP)的影响很大,这是一个Haskell首先开发的一个想法,所以你应该找到很多类似的库。 F#版本是不太纯,以便更加实用于.NET编程。
As mentioned in comments, the F# model is heavily influenced by functional reactive programming (FRP) which is an idea that was first developed in Haskell, so you should find a plenty of similar libraries. The F# version is "less pure" in order to be more practical for .NET programming.
这篇关于在F#中Control.Observable和Control.Event模块之间有什么相似之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!