简体   繁体   English

.Net和In C#上的多态数字

[英]Polymorphic Numerics on .Net and In C#

It's a real shame that in .Net there is no polymorphism for numbers, ie no INumeric interface that unifies the different kinds of numerical types such as bool, byte, uint, int, etc. In the extreme one would like a complete package of abstract algebra types. 真的很遗憾,在.Net中没有数字的多态性,即没有统一不同类型的数字类型的数字接口,如bool,byte,uint,int等。在极端情况下,我们想要一个完整的抽象包代数类型。

Joe Duffy has an article about the issue: Joe Duffy有一篇关于这个问题的文章:

http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx

How would you express this in C#, in order to retrofit it, without having influence over .Net or C#? 您如何在C#中表达这一点,以便在不影响.Net或C#的情况下对其进行改造?

I have one idea that involves first defining one or more abstract types (interfaces such as INumeric - or more abstract than that) and then defining structs that implement these and wrap types such as int while providing operations that return the new type (eg Integer32 : INumeric; where addition would be defined as 我有一个想法,首先要定义一个或多个抽象类型(接口,如INumeric - 或者比它更抽象),然后定义实现这些的结构并包装类型如int,同时提供返回新类型的操作(例如Integer32: INumeric;其中添加将被定义为

public Integer32 Add(Integer32 other)
{
    return Return(Value + other.Value);
}

I am somewhat afraid of the execution speed of this code but at least it is abstract. 我有点害怕这段代码的执行速度,但至少它是抽象的。

No operator overloading goodness... 没有运营商超载良好......

Any other ideas? 还有其他想法吗?

.Net doesn't look like a viable long-term platform if it cannot have this kind of abstraction I think - and be efficient about it. .Net看起来不像一个可行的长期平台,如果它不能拥有我认为的这种抽象 - 并且对它有效。

Abstraction is reuse. 抽象是重用。

update: 更新:

This is an example implementation type signature so far: 到目前为止,这是一个示例实现类型签名:

public struct Integer32 : INumeric<Integer32, Int32>, IOrder<Integer32, Int32>

Compensating for the lack of covariant return types. 补偿缺乏协变返回类型。

Someone has already gone to the effort of writing something which may solve your delemma. 有人已经开始写一些可以解决你的问题的东西了。 It's called Generic Operators , and is available in the Miscellaneous Utility Library . 它被称为通用运算符 ,可在杂项实用程序库中找到

The csharp language team is already looking into this. csharp语言团队已经在研究这个问题。 If you want a view onto the future of type classes in C# start reading at 如果你想在C#中开始读取类型类的未来视图

https://github.com/dotnet/csharplang/issues/164 https://github.com/dotnet/csharplang/issues/164

在此输入图像描述

It seems to have the support of Mads Torgesson so it's not just a random post by a wandering Haskell fanboy. 它似乎得到了Mads Torgesson的支持,所以它不仅仅是一个流浪的Haskell粉丝的随机帖子。

The example given of a typeclass or shape in C# land is 在C#land中给出类型类或形状的例子是

public shape SGroup<T>
{
    static T operator +(T t1, T t2);
    static T Zero { get; }
}

notice this is not like an interface. 注意这不像是一个界面。 It is declaring static method that belong to SGroup. 它声明属于SGroup的静态方法。 Read on for more details and discussion . 继续阅读以了解更多详情和讨论

If you plan to use C# 4.0 then you can easily simulate generic mathematical operations using dynamic . 如果您打算使用C#4.0,那么您可以使用dynamic轻松模拟通用数学运算。 Here is an example of a simple addition function (for more information see this blog ): 以下是一个简单添加功能的示例(有关更多信息, 请参阅此博客 ):

public static T Add<T>(T a, T b) {
  dynamic ad = a;
  dynamic bd = b;
  return ad + bd;
}

I haven't played with this, so I can't say much about the performance. 我没有玩过这个,所以我不能多说性能。 There will certainly be some performance price for using dynamic, but I think the DLR should be able to do very effective optimizations if you'll invoke the function multiple times. 使用动态肯定会有一些性能价格,但我认为如果您多次调用该函数,DLR应该能够进行非常有效的优化。 In fact, I won't be surprised if it had similar performance profile as Generic Operators mentioned above. 事实上,如果它具有与上面提到的通用运营商类似的性能特征,我不会感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM