语境:
我正在 C# 中实现一个 BigRational 结构,它需要各种算术和比较重载。许多重载中的代码看起来完全相同,因为使用了var. 最重要的是,我收到了 CA2225 的警告,其中指出各种操作员需要一种“友好的替代命名”方法。请参阅https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2225
由于许多算术运算符被重载
- (BigRational, BigRational) -> BigRational
- (BigRational, BigInteger) -> BigRational
- (BigInteger, BigRational) -> BigRational
- (BigRational,长)-> BigRational
- (长,BigRational)-> BigRational
- (BigRational, ulong) -> BigRational
- (ulong, BigRational) -> BigRational
和比较运算符
- (BigRational, BigRational) -> bool
- (BigRational, BigInteger) -> bool
- (BigInteger, BigRational) -> bool
- (BigRational, long) -> bool
- (long, BigRational) -> bool
- (BigRational, ulong) -> bool
- (ulong, BigRational) -> bool
这会导致大量重复的代码和样板。
问题:
有没有一种简单的方法来使用 C# 9 样式生成器来实现这个?
旁观:
如果 C# 可以在语言中内置重载生成,那就太好了
public static BigRational Add<T>(BigRational augend, T addend)
where T overloads: BigInteger, long, ulong
{
// use var in code...
}
相当于
public static BigRational Add(BigRational augend, BigInteger addend)
{
}
public static BigRational Add(BigRational augend, long addend)
{
}
public static BigRational Add(BigRational augend, ulong addend)
{
}
这也将允许为不共享基类或接口但具有非常相似的 API 的类型编写单个函数。