Wednesday, April 12, 2006

Variable Meta data: Ways to preserve units of measure in variables

In order to enforce safety as well as robust behaviour, there should be ways to specify the units of a variable. E.g. a float that has an inch. In this case the inch cannot be compared to a meter.

 

These issues should be handled without performance implications. Having specific types like Inch and Meters may be cumbersome and slow in execution. Therefore, the ideal would be to tag or store attributes or metadata about the variables and depending on the context handle issues like convention. In a static context these types should not have more execution overheads than dealing with normal primitive types, provided they are compatible. In case they are not compatible conventions will need to take place.

 

The classifications attributed can be stored as a tree like lengths of which meters and inches are subtypes. Length can be general classification where as meters an inches can be specific units of the measure. Any comparison or computation using variables of the same units will be possible without overhead. In case of the same classification but different units a conversion need to be done. In order to facilitate conversion there should be a preferred representation (E.g. like SI units) and an error associated with the convention. On computation the LHS or lparam will have the associated error of the computation as an attributed which can be retrieved. If the variables are under different classifications then an exception or error will occur if it is detected in a dynamic context else a compile time warning should at least be raised in a static context.

 

The dynamic context will only come into play if items of unknown type or if objects are used to represent primitive types. When up casting or down casting happens special handling will be needed.  If down casting is used then the classification should be equal. If the units are different then conversion should happen on access or assignment. Casting like reinterpret casts of items with information about units they represent will become very problematic, so they can be disallowed when designing languages which use it.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home