Functional Programming w/ C# ~ ラムダ式の出力からの型推論

下記のような、前項の結果を参照・利用する拡張メソッドを定義しようとしていて気づいた。

|cs| // 定義 static IEnumerable SelectPrevRef<TSource, TResult>(this IEnumerable source, Func<TResult, TSource, TResult> func) where TResult : new() { var prev = new TResult();

return source.Select( x => prev = func(prev, x) ); }

// 利用 // 1 ~ 9 のある数字が最大 n 桁連なる数を modulus で割った余りが 0 となるものの最大値を求める

int n = xxxx; // 桁数 int digit = 1; // 1 ~ 9 の数字 int modulus = xxxx; // なんらかの数

Enumerable,Range(1, n) .SelectPrevRef( (prev, length) => new { Length = length, Rem = (prev.Rem * 10 + digit) % modulus; } ) .Where( x => x.Rem == 0 ) // ... 以下略 ||< Func<TResult, TSource, TResult> から出力の型が匿名型 new { int Length, int Rem } であることは理論上は解るはず。だからこの匿名型の初期値は外から与えずメソッド定義内で与えたい。しかし TResult 型を単独のメソッド引数として与えないと型推論できないようだ。 TypeScript ならできるのに。C# 言語仕様検討チームにプルリクエストしても、これまた、型推論の負荷が高まるからダメとか反対されるのだろうな。