かなり違うので、これを新しい回答として追加します...
したがって、そのオペレーターのオーバーロードルートを実行することに専念している場合は、次のようにする必要があります(まあ、少なくとも1つの方法です)...正直なところ、私はこのアプローチが好きではありません-クエリの記述がはるかに簡潔になりますが、DSL アプローチには同様の「簡潔さ」がありますが、演算子のオーバーロードに依存しないという意味ではるかに明確です。
public static class ArithmeticStreamExt
{
public static ArithmeticStream Wrap(this IObservable<double> src)
{
return new ArithmeticStream(src);
}
public static ArithmeticStream Const(this double constValue)
{
return new ArithmeticStream(Observable.Return(constValue));
}
}
public class ArithmeticStream
{
private IObservable<double> _inner;
public ArithmeticStream(IObservable<double> source)
{
_inner = source;
}
public IObservable<double> Source {get { return _inner; }}
public static ArithmeticStream operator +(
ArithmeticStream left,
ArithmeticStream right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right._inner, (l, r) => l + r));
}
public static ArithmeticStream operator -(
ArithmeticStream left,
ArithmeticStream right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right._inner, (l, r) => l - r));
}
public static ArithmeticStream operator *(
ArithmeticStream left,
ArithmeticStream right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right._inner, (l, r) => l * r));
}
public static ArithmeticStream operator /(
ArithmeticStream left,
ArithmeticStream right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right._inner, (l, r) => l / r));
}
public static ArithmeticStream operator +(
ArithmeticStream left,
IObservable<double> right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right, (l, r) => l + r));
}
public static ArithmeticStream operator -(
ArithmeticStream left,
IObservable<double> right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right, (l, r) => l - r));
}
public static ArithmeticStream operator *(
ArithmeticStream left,
IObservable<double> right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right, (l, r) => l * r));
}
public static ArithmeticStream operator /(
ArithmeticStream left,
IObservable<double> right)
{
return new ArithmeticStream(
left._inner.CombineLatest(right, (l, r) => l / r));
}
}
そしてテストリグ:
void Main()
{
var s1 = new Subject<double>();
var s2 = new Subject<double>();
var s3 = new Subject<double>();
var s4 = new Subject<double>();
var result = (s1.Wrap() + s2) / s3 + (s4.Wrap() * 2.0.Const());
using(result.Source.Subscribe(Console.WriteLine))
{
s1.OnNext(1.0);
s2.OnNext(2.0);
s3.OnNext(3.0);
s4.OnNext(4.0);
}
}