akkadotnet/akka.net — pull request #346 by mattnischan (2014-08-29 03:36:53+00:00)
Title: Added compilable expression based PatternBuilder.

So, as opposed to reopening the PatternMatch issue specifically, I thought I'd throw in my idea for a performance oriented pattern matcher. Some tradeoffs and notes:

* It's not quite as idiomatically functional. You need to build the pattern and store it somewhere, but after that it is a compiled delegate that lives for as long as the actor lives.
* Compilation of the delegate is slow (well, relatively speaking, it's still quite fast), but matching is the same performance as hand writing all the if/else is/as statements.
* It does retain a nice friendly fluent interface.

Usage is like so:

private Matcher messageHandler;
public SomeActor()
{
messageHandler = PatternBuilder.Match()
.With(s => Console.WriteLine(s))
.With(i => DoSomethingElse(i))
.Default(obj => DoDefault(obj))
.Compile();
}

protected override void OnReceive(object message)
{
var handled = messageHandler(message);
}

Feedback?