Aconcagua measure library - storing BaseUnits

折月煮酒 提交于 2019-12-11 09:08:07

问题


What is the best practice to store base units?

Let's say I want to have a mile unit. So, I implement Number>>#miles, but what is the implementation?

The problem is that: (2 * (BaseUnit named: 'mile')) ~= (2 * (BaseUnit named: 'mile')), so it seems the mile base unit must be a singleton.

So I'd have something like:

Number>>#miles
    ^ self * Mile uniqueInstance

Am I on the right track, or can you think of a better way?


回答1:


Yes the mile base unit must be a singleton, you can take a look at the Chalten framework which is using Aconcagua, in particular the TimeUnitsGlobal class. For the Number method part, in Chalten it's done this way :

Number>>#daysMeasure
    ^TimeUnits day with: self

Although I have an issue with the way it's done there because I can't found a way to use Fuel with such units after that.




回答2:


units are not really singletons but they use the original #= to see if two units are the same and the default implementation of #= verifies identity with #==, but that can be overridden if necessary. The reason I did it that way is because I thought it was the most generic implementation. The easiest way to use them is to store units in global variables, therefore you can define:

Smalltalk at: #Mile put: (BaseUnit named: 'mile' etc etc).

And then you can do 2*Mile witch makes a lot of sense because... it is like saying that "the knowledge of mile is global"

Another way to do it is how Chalten does it. That is to have a class that knows each unit and you can access them with messages like "TimeUnits day"

The idea is to avoid having to create a class per unit that does not make any sense... Another possibility is to 1) modify #= on Unit and use the uni's name to verify if two units are the equal 2) Subclass BaseUnit and do 1) :-)

If you have problems with Fuel, it is because you are not saving the root object that knows each unit, but once you do it the problem should be solved.

Hernan.



来源:https://stackoverflow.com/questions/23744072/aconcagua-measure-library-storing-baseunits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!