比较特殊的仿制药?在C#泛型算术可能的解决方案 [英] Less generic generics? A possible solution for arithmetic in C# generics

查看:126
本文介绍了比较特殊的仿制药?在C#泛型算术可能的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,在C#泛型不允许进行算术任何理智的方式。有可用的尴尬解决办法,但他们都不是很整齐,他们都降低性能。
根据这次采访,用算术类型的接口是不可能实现,所以这样的一个解决方法建议。




但你可以做的是有你的矩阵需要作为一个参数一个计算器,在计算器,已多次调用的方法。你去实现这一点,你把它传递给矩阵。




我为什么要告诉一个高级编程语言如何添加和繁殖号码?



为什么不干脆让一个一般被限制在一个类型列表?

例如:

 类矩阵< T>其中T:INT,长,浮动,双



语法当然可以是不同的。但是,编译器仅需要检查的类型是在列表中,并且在所有类型的,这应该是比显然太困难接口建议简单得多用于工作的操作人员。



是否有任何明显的原因,为什么这不能实施?


解决方案

我认为你正在寻找在OOP函数式编程语言的功能。如果是这样的话,那么使用FP。



例如,在Scala中,可以更换运营商,所以现在可以相加或相乘的矩阵。



这是各种语言的情况下,但不是所有的OOP语言。



为什么要仿制药能够做加法时,这不是明摆着如何相加或相乘?举例来说,如果我有一个类对数,再乘以只是添加数字,但是,怎么可能一个编译器知道?



在C#中,你可以做运营商超载,这应该解决你所抱怨的问题:
http://msdn.microsoft.com/en-us/library/aa288467%28VS.71%29.aspx



C#团队来就怎么做仿制药,它不应该处理数学是我上面的例子将显示的决定,但他们并允许您以帮助编译器知道该怎么做数学运算,因此责备他们不要求他们为你第一个观点是非常危险的,因为决定已经作出,仿制药是因为它们。它不会改变,现在,我相信了。


Currently, generics in C# do not allow any sane way to perform arithmetic. There are awkward workarounds available, but none of them are very neat and all of them reduce performance. According to this interview, an interface with arithmetic types is not possible to implement, and so one such workaround is suggested.

But what you could do is have your Matrix take as an argument a Calculator, and in Calculator, have a method called multiply. You go implement that and you pass it to the Matrix.

Why should I have to tell an advanced programming language how to add and multiply numbers? [Edited due to popular demand]

Why not simply allow a Generic to be restricted to a list of types?

Eg.

class Matrix<T> where T : int,long,float,double

The syntax could of course be different. But the compiler needs only to check that the type is on the list, and that the operators used work on all types, which should be much simpler than the apparently-too-difficult interface suggestion.

Are there any obvious reasons as to why this cannot be implemented?

解决方案

I think you are looking for the functionality of a functional programming language in OOP. If that is the case, then use FP.

For example, in Scala, you can replace the operators, so now it can add or multiply your matrix.

This is the case in various languages, but not all OOP languages.

Why should generics be able to do addition when it isn't obvious how to add or multiply? For example, if I have a class of logarithms, then to multiply is just adding the numbers, but, how could a compiler know that?

In C# you can do operator overloading, which should solve the problem you are complaining about: http://msdn.microsoft.com/en-us/library/aa288467%28VS.71%29.aspx

The C# team came to decisions on how to do generics, and it shouldn't handle math as my example above would show, but they did allow for you to help the compiler know how to do the math operations, so blaming them for not asking them for your opinion first is very risky, as the decision has been made, generics are in as they are. It won't be changed now, I believe.

这篇关于比较特殊的仿制药?在C#泛型算术可能的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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