كيف سيبدو رمز Scala هذا في Idiomatic C#؟
-
30-09-2019 - |
سؤال
لقد صادفت رمز Scala الجميل حقًا أثناء البحث عن XMPP لمشروع .NET الذي نعمل عليه:
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
}
}
(تم أخذ هذا من http://github.com/communitivity/minimalscalaxmppcomponent/blob/master/src/org/communitivity/echoxmpp/main.scala)
يبدو أن الممثل والمطابقة للأنماط طريقة رائعة حقًا لكتابة المكونات بطريقة قابلة للتطوير. لم أتمكن من العثور على أي مكتبات ممثلين C# تبدو ناضجة ، ويبدو أن التعلم Axum مبالغة.
ماذا ستكون الطريقة الصحيحة لمهاجمة هذا في C#؟ (تجاهل رمز XMPP المحدد ، بالطبع - أنا مهتم أكثر بما سيبدو عليه إصدار C# من الممثلين ومطابقة الأنماط).
المحلول
هناك ممثل مثل المكتبات Avaliable لـ .NET See Retlang لأحد ، أيضا F# الوكلاء متشابهة.
انظر أيضا هذا سؤال.
لا تنتمي إلى StackOverflow