Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In C, I wonder if you could do something with functions and macros?

Say you need to represent velocity in a transportation simulation. You could have a function, velocity, that looks like this:

  double velocity(double v, char * of_what)
You use it to wrap constrained values. E.g.,

  double v_jogger = velocity(8.0, "human");
  double v_car = velocity(65.0, "city car");
velocity() simply returns the first argument, after doing validity checking based on the second argument.

You probably couldn't reasonably use this everywhere that you would use actual constrained types in a language that has them, but you could probably catch a lot of errors just using them in initializers.



The problem you'd have is that doing any operations on such a value could take it outside the bounds of the "type".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: