コールバックベースのスタイルを使用してプロプライエタリバスに接続して通信するためのJavaAPIが提供されました。私は現在、概念実証アプリケーションをscalaに実装しており、もう少し慣用的なscalaインターフェイスを作成する方法を模索しています。
典型的な(簡略化された)アプリケーションは、Javaでは次のようになります。
DataType type = new DataType();
BusConnector con = new BusConnector();
con.waitForData(type.getClass()).addListener(new IListener<DataType>() {
public void onEvent(DataType t) {
//some stuff happens in here, and then we need some more data
con.waitForData(anotherType.getClass()).addListener(new IListener<anotherType>() {
public void onEvent(anotherType t) {
//we do more stuff in here, and so on
}
});
}
});
//now we've got the behaviours set up we call
con.start();
Scalaでは、(T => Unit)からIListenerへの暗黙の変換を明らかに定義できます。これにより、物事が少し読みやすくなります。
implicit def func2Ilistener[T](f: (T => Unit)) : IListener[T] = new IListener[T]{
def onEvent(t:T) = f
}
val con = new BusConnector
con.waitForData(DataType.getClass).addListener( (d:DataType) => {
//some stuff, then another wait for stuff
con.waitForData(OtherType.getClass).addListener( (o:OtherType) => {
//etc
})
})
これを見ると、scalazの約束とf#非同期ワークフローの両方を思い出しました。
私の質問はこれです:
これを理解のために、または同様に慣用的なものに変換できますか(これは俳優にもかなりうまくマッピングされるべきだと思います)
理想的には、次のようなものを見たいです。
for(
d <- con.waitForData(DataType.getClass);
val _ = doSomethingWith(d);
o <- con.waitForData(OtherType.getClass)
//etc
)