Comment

Comment by pjungwir

Random question for the type experts out there: is there any language that lets me track the units of my numeric variables? For instance, something like this:

    float<km> drop(float<m> x0, float<s> duration) {
      float<m> x = x0;
      float<s> t = 0;
      float<m/s> v = 0;
      float<m/s^2> g = -10;
      float<s> dt = 0.01;
      while (t < duration) {
        v += g*dt;
        x += v*dt;
        t += dt;
      }
      return x * (1<km>/1000<m>);  // abbrev for a cast: (float<km/m>).001
    }
Then I want the compiler to check that I'm not mixing up my units. It seems like this would be really useful, but I've never seen it before.
parent pjungwir 12 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton