使用现实世界的单位,而不是类型 [英] Using real world units instead of types

查看:154
本文介绍了使用现实世界的单位,而不是类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多计算项目涉及到很多现实世界的单位:

I have a project with many calculations involving a lot of real world units :


  • 距离;

  • 温度;

  • 流量;

  • ...

  • Distance;
  • Temperature;
  • Flow rate;
  • ...

本项目涉及复杂和众多的计算公式。

This project involves complicated and numerous calculation formulas.

这就是为什么我应该使用自定义类型,如温度距离的...可以很好的为代码的可读性。例如:

That's why I supposed the use of custom types like Temperature, Distance... can be good for code readability. For example:

Temperature x = -55.3;
Meter y = 3;

var x = new Temperature(-55.3);



我试图让一个使用双内值温度等级。

I tried to make a Temperature class that uses a double internal value.

public class Temperature
{
    double _Value = double.NaN;

    public Temperature() { }

    public Temperature(double v) {
        _Value = v;
    }

    public static implicit operator Temperature(double v) {
        return new Temperature(v);
    }
}



不过是类可空。这意味着这样的:

But class are nullable. This mean that something like :

Temperature myTemp;



是正确的,并会为空。我不想这样。我不想使用结构,因为他们太有限:

is "correct" and will be null. I dont want this. I dont want to use structs because they are too limited :


  • 他们不能使用无参数的构造函数,也没有实例字段intializers像双_Value = double.Nan; 来定义默认值(我的魔杖默认标的双重价值是NAN)

  • 他们不能从类继承,他们只能实现接口

  • They cannot use parameterless constructor nor instance field intializers like double _Value = double.Nan; to define a default value (I wand default underlying double value to be NaN)
  • They cannot inherits from classes, they only can implement Interfaces

他们,我不知道是否有办法告诉C#:

Them I wonder whether there is a way to tell C#:

Temperature myTemp = 23K; // C# does not implement anything to make K unit...



但我知道C#不处理。没有自定义单元

but I know C# does not handle no custom units.

Temperature myTemp = new Kelvin(23); // This might work



所以,我想我可以创建一个从温度遗传了两个摄氏度和Kelvin类, 。然后我开始这个想法很值得怀疑,因为它涉及到很多的编码和测试的

So I imagine I could create two Celsius and Kelvin classes that inherits from Temperature, and then I started to wonder if the idea really worth it, because it involves a lot of coding and testing.

这是我想开始讨论:

会在我的代码,而不是.NET类型使用现实世界的单位将是好事还是坏事?难道这个人了吗?什么是陷阱和最佳做法?或者我应该更好地一直从这个远离并使用标准的.NET类型?

Would the use of real world units in my code instead of .NET types would be a good thing or not ? Did anyone this already ? What are the pitfalls and the best practices ? Or should I better keep stay away from this and use standard .NET types ?

推荐答案

要实现这一目标是使用一种方法(你的情况温度)的基本目标与专门的基本对象的 TemperatureTraits 类组成。通过类比C ++中,字符串等价类的basic_string 实际上是有模板类模板(在C#条款通用)参数不仅为字符串元素(字符,宽字符)而且可以在类的行为给定类型的字符串元素的阐述一个traits类(如 char_traits )。

One way to achieve this would be to use composition of the basic object (Temperature in your case) with a TemperatureTraits class that specializes the basic object. By analogy to C++, the String equivalent class basic_string is actually a class template (generic in C# terms) that has template parameters not only for the string element (char, wide char) but also a traits class that elaborates on how the class behaves for a given type of string element (e.g. char_traits).

在你的情况,你可以定义一个通用的如

In your case, you might define a generic like

公共类MeasurableWithUnits< M级衡量,U类单位和GT;

然后实施将不仅取决于可测量的类,但也对单位类。如何用这将是在实践中,将取决于有多少这样的对象可以作出真正的通用 - 横渡可测量组合哪些操作是常见的单位

and then implementation would depend not only on the measurable class but also on the units class. How useful this would be in practice would depend on how much of such an object could be made truly generic - what operations are common across combinations of Measurable and Units?

有是C#性状的研究论文的这里,如果这种方法看起来很有趣。

There is a research paper on C# traits here, if this approach looks interesting.

这篇关于使用现实世界的单位,而不是类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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