在C#中的類和接口可以有event
S:什麼是Scala中慣用的等效C#的事件
public class Foo
{
public event Action SomethingHappened;
public void DoSomething()
{
// yes, i'm aware of the potential NRE
this.SomethingHappened();
}
}
這有利於以最小的樣板代碼基於推送的通知,並允許多用戶模式,使很多觀察者都可以聽到這個事件:
var foo = new Foo();
foo.SomethingHappened +=() => Console.WriteLine("Yay!");
foo.DoSomething(); // "Yay!" appears on console.
在Scala中是否有相當的習語?我正在尋找的是:
- 最小的樣板代碼
- 單出版商,多用戶
- 連接/斷開用戶的斯卡拉文檔中使用的
例子將是美好的。我不想在Scala中尋找C#事件的實現。相反,我在斯卡拉尋找等效的成語。
這可能是有益的:http://stackoverflow.com/questions/6814246/in-scala-how-would-i-combine-event-driven-programming-with-a-functional-approac – daryal