In C#, classes and interfaces can have event
s:
public class Foo
{
public event Action SomethingHappened;
public void DoSomething()
{
// yes, i'm aware of the potential NRE
this.SomethingHappened();
}
}
This facilitates a push-based notification with minimal boilerplate code, and enables a multiple-subscriber model so that many observers can listen to the event:
var foo = new Foo();
foo.SomethingHappened += () => Console.WriteLine("Yay!");
foo.DoSomething(); // "Yay!" appears on console.
Is there an equivalent idiom in Scala? What I'm looking for is:
Examples of its use in Scala documentation would be wonderful. I'm not looking for an implementation of C# events in Scala. Rather, I'm looking for the equivalent idiom in Scala.
Idiomatic way for scala is not to use observer pattern. See Deprecating the Observer Pattern.
Take a look at this answer for implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With