Monday, March 20, 2023

Variables have type. They should have units too.

A conversation on computer science I had today with a friend.


 OK, one of the things that always bothered me with computer languages is that data has a type, but not a unit.


I completely agree

I have thought about that as well


I would love to see a computer language that could automatically convert miles to km if used miles in the calculation, but the result was in km


It would catch errors much earlier in the process


at least throw an error

orange = 5 apples....   unit error, compilation halted.

One of the space craft nasa sent up crashed at full speed into the martian desert because one programmer used miles and another used km.


Wow, that's crazy. I'm surprised they didn't catch the trajectory issue earlier on.

But yes. 5 = 5 is rarely what is being compared

Only when you are comparing truly unitless numbers, which is rare.

It's almost always 5 of something = 5 of something.


Yeah, I have only found a few formulas where all the units cancel out and give you a number

I think ratio is the main one

but even that doesn't mater if you have 5mi/5km.  The answer is not 1. L


You could do this on top of most languages

const num = new Number.Miles()

But that doesn't mean someone somewhere doesn't convert it back into a raw integer

And you don't get all the standard operators


but I was thinking if the compiler had an extendable library of unit conversions it could do the right thing.


Yep, doing it "in" the language itself at the compiler level would be better.


nods

and if you had an error you could define a conversion to fix the error.

then the assumption could be spelled out.


I like the idea of

const distance = 5 miles;

const time = 30 minutes;

const speed = distance / time;

// speed -> 10 miles per hour

time > distance

// Error! No comparison operator between minutes and distance.

That would be amazing


exactly!


Instead of doing * 1000 * 60 * 60 * 24 you could just configure what units you want as output.

And have automatic localization for countries and regions


So then you could add a line that defined the formula and the compiler could do that math.

and the output box could have the local defined unit and just convert, as you say.

math happening at the web brower output box L

define formula miles = kilometers / 1.62;

and the compiler could be smart enough to know that km = miles * 1.62


Would only numeric types have units?

Would strings / objects / arrays have units?


a string could be xml or tcl data or csv


Ahh I like it


or a language

then if you have english input and french localization it could translate using ai

it could be cool to convert tcl{ tags} to <xml>tags</xml>

I am saving this conversation to a text file [en]. L

No comments:

Post a Comment