3

I came across this really nice looking Scala code while researching XMPP for a .Net project we're working on:

object Main {

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]) :Unit = {
      new XMPPComponent(
        new ComponentConfig() {
            def secret() : String = { "secret.goes.here" }
            def server() : String = { "communitivity.com" }
            def subdomain() : String = { "weather" }
            def name() : String = { "US Weather" }
            def description() : String = { "Weather component that also supported SPARQL/XMPP" }
        },
       actor {
        loop {
            react {
                case (pkt:Packet, out : Actor) =>
                    Console.println("Received packet...\n"+pkt.toXML)
                    pkt match {
                        case message:Message =>
                            val reply  = new Message()
                            reply.setTo(message.getFrom())
                            reply.setFrom(message.getTo())
                            reply.setType(message.getType())
                            reply.setThread(message.getThread())
                            reply.setBody("Received '"+message.getBody()+"', tyvm")
                            out ! reply
                        case _ =>
                            Console.println("Received something other than Message")
                    }
                 case _ =>
                    Console.println("Received something other than (Packet, actor)")
            }
        }
       }
    ).start
  }
}

(This was taken from http://github.com/Communitivity/MinimalScalaXMPPComponent/blob/master/src/org/communitivity/echoxmpp/Main.scala)

The actor and pattern matching stuff looks like a really nice way to write components in a scalable way. I haven't been able to find any C# actors libraries that look mature, and learning Axum seems like overkill.

What would be the right way to attack this in C#? (ignoring the XMPP specific code, of course - I'm more interested in what the C# version of actors and pattern matching would look like).

pmn
  • 1,640
  • 2
  • 14
  • 15
  • I'm not really sure what qualifies as an answer here. C# has some extensions that promote a functional programming style but it is still an imperative language. I think the functionality in this sample is not that hard to replicate, but it will either be imperative style, or kludgy to mimic functional language features. –  Oct 14 '10 at 17:31

1 Answers1

3

There are Actor like libraries avaliable for .NET see retlang for one, also F# agents are similar.

Also see this question.

Community
  • 1
  • 1
oluies
  • 17,694
  • 14
  • 74
  • 117
  • I guess retlang is probably the closest workable solution without changing programming languages, thanks – pmn Oct 14 '10 at 18:35