多态Numerics的对.Net和在C# [英] Polymorphic Numerics on .Net and In C#

查看:105
本文介绍了多态Numerics的对.Net和在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个真正的耻辱,在.net中没有多态性数字,即没有INumeric接口统一了不同类型的数值类型,如布尔,字节,UINT,INT等。在极端的人会喜欢一个完整的包抽象代数类型。

It's a real shame that in .Net there is no polymorphism for numbers, i.e. 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.

乔·达菲已经对这个问题的文章:

Joe Duffy has an article about the issue:

<一个href="http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx" rel="nofollow">http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx

你将如何前preSS这在C#中,为了改造它,而不必影响力的.Net或C#?

How would you express this in C#, in order to retrofit it, without having influence over .Net or C#?

我有一个想法,包括首先定义一个或多个抽象类型(如INumeric接口 - 或比这更抽象的),然后确定实现这些和包裹类型,如同时提供返回新类型的操作INT(结构例如整型:INumeric;其中除了将被定义为

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 (e.g. Integer32 : INumeric; where addition would be defined as

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

我有些怕这怕code中的执行速度,但至少它是抽象的。

I am somewhat afraid of the execution speed of this code but at least it is abstract.

没有操作符重载善良...

No operator overloading goodness...

任何其他的想法?

净看起来并不像一个可行的长期平台,如果不能有这种抽象的,我认为 - 并且是有效的呢

.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.

抽象是重用。

更新:

这是一个示例实现类型签名至今:

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.

推荐答案

有人已经去写一些东西,可以解决您的delemma的努力。这就是所谓的通用操作符,并提供了的其他工具库

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.

这篇关于多态Numerics的对.Net和在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆