Unit Type value classes (e.g., miles, gallons, kilometers, liters)

How are the unit type value classes properly used; i.e. given miles how do I get kilometers? Every variation I try fails to compile so I’ve just been doing the conversions myself.

[Edit addition: OS X 10.3.9]

Clumsily. You can only create a value by coercing number to the desired unit type (e.g. 3 as miles); you can’t declare them as specifiers (e.g. miles 3) although AppleScript confusingly displays them that way. And once you’ve got a unit type value you can’t do anything really useful like arithmetic with it, which is a great pity; all you can do is coerce it to another unit type or back to a number.

Example:

3 as miles as kilometers as number --> 4.828031992661

…Which leads to novel constructions like

display dialog ((((text returned of (display dialog "Enter mileage to be converted to kilometers:" default answer "") as number) as miles) as kilometers) as text) & " kilometers"

No wonder I couldn’t get it. “Clumsily” describes it accurately.

Novel indeed. :cool:

The unit conversion coercions are useful - though, as hhas said, clumsy - if you don’t know the conversion formulae. But if you do know the figures, that would be an easier and faster way to go. You could even define your own conversion variables:

set milesAsKilometres to 1.609343997554

set kilometrage to milage * milesAsKilometres

When the unit coercions were introduced sometime around OS 8.5 or 8.6, it was considered cool that you could use either “er” or “re” spellings for metric units. (They compile as American spellings.) However, ‘gallons’ refers exclusively to the US gallon, not the larger Imperial gallon used in other parts of the word. For that, you have to use your own maths anyway.

[OT] I see the adverts at the bottom of this page are based on words used in the posts. Fiendish. :expressionless:

As I suspected, they changed as soon as I posted that! :rolleyes:

The “roll your own” approach was the one I had taken. It just seemed a shame that the built-in versions were not accessible. I’ll stick to my own, though. Thanks all.