如何避免在.NET中为不同的数字类型编写重复的代码 [英] How to avoid writing repetitive code for different numeric types in .NET

查看:86
本文介绍了如何避免在.NET中为不同的数字类型编写重复的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写通用的Vector2类型,该类型将适用于float,double等类型,并使用算术运算。有没有机会用C#,F#,Nemerle或任何其他或多或少成熟的.NET语言来做到这一点?

I am trying to write generic Vector2 type which would suite float, double, etc. types and use arithmetical operations. Is there any chance to do it in C#, F#, Nemerle or any other more or less mature .NET language?

我需要


  • (1)良好的性能(与编写单独的
    Vector2Float,Vector2Double等类相同),

  • (2),这将允许
    代码看起来不错(我不想在
    运行时为每个类发出代码)

  • (3 ),这将进行尽可能多的编译时间检查。

出于第1和第3个原因,我不希望使用动力学。现在,我正在检查F#和Nemerle。

For reasons 1 and 3 I would not like to use dynamics. Right now I am checking F# and Nemerle.

UPD:我希望这种类型的数学代码很多。但是,如果可能的话,我宁愿将代码放在扩展方法中。

UPD: I expect to have a lot of mathematical code for this type. However, I would prefer to put the code in extension methods if it is possible.

UPD2:'etc'类型包括int(我将实际上怀疑我会使用)和十进制(我想我可能会使用,但现在不使用)。使用扩展方法只是一个问题,如果有充分的理由,请告诉。

UPD2: 'etc' types include int(which I actually doubt I would use) and decimal(which I think I might use, but not now). Using extension methods is just a matter of taste - if there are good reasons not to, please tell.

推荐答案

正如丹尼尔(Daniel)所述,F#具有称为<静态>静态解析类型参数的功能,这超出了C#中普通.NET泛型的功能。诀窍是,如果将功能标记为 inline ,则F#自动生成专用代码(有点像C ++模板),然后可以使用F#类型系统的更强大功能来编写通用数学。

As mentioned by Daniel, F# has a feature called statically resolved type arguments which goes beyond what you can do with normal .NET generic in C#. The trick is that if you mark function as inline, F# generates specialized code automatically (a bit like C++ templates) and then you can use more powerful features of the F# type system to write generic math.

例如,如果您编写一个简单的add函数并将其设置为 inline

For example, if you write a simple add function and make it inline:

let inline add x y = x + y;;    

类型推断将打印以下类型:

The type inference prints the following type:

val inline add :
  x: ^a -> y: ^b ->  ^c
    when ( ^a or  ^b) : (static member ( + ) :  ^a *  ^b ->  ^c)

您可以看到,推断类型非常复杂-它指定了一个成员约束,该成员约束需要两个参数之一来定义 + 成员(标准.NET类型也支持此成员)好处是,可以完全推断出它,因此您几乎不必编写难看的类型定义。

You can see that the inferred type is fairly complex - it specifies a member constraint that requires one of the two arguments to define a + member (and this is also supported by standard .NET types) The good thing is that this can be fully inferred, so you will rarely have to write the ugly type definitions.

正如评论中提到的那样,我写了一篇文章编写通用数字代码,其中涉及更多内容有关如何在F#中执行此操作的详细信息。我不认为这可以在C#中轻松完成,而您在F#中编写的内联函数只能从F#中调用(从C#调用它们本质上将使用动态)。但是,您绝对可以用F#编写通用数值计算。

As mentioned in the comments, I wrote an article Writing generic numeric code that goes into more details of how to do this in F#. I don't think this can be easily done in C# and the inline functions that you write in F# should only be called from F# (calling them from C# would essentially use dynamic). But you can definitely write your generic numerical computations in F#.

这篇关于如何避免在.NET中为不同的数字类型编写重复的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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