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

But perhaps a better type system can still be helpful. E.g. F# supports units of measure, so you can declare 'dollar' as a unit, and write

    let calc_total (meal_cost : float<dollar>) (tax : float) (tip : float) =
       meal_cost * (1.0+tax) * (1.0+tip)
This way it is at least clear from the types that the numbers are meant to be multiplied, not added.


I never used F#. In your example, would the code:

    meal_cost + tax
trigger a compiler exception?


Yes, you get

    The type 'float' does not match the type 'float<dollar>'
There is an online typechecker, so one can try out F# without installing it: http://www.tryfsharp.org/Learn/scientific-computing#units-of...


And how about:

    meal_cost * tax
?

This one makes sense in the real world, so I suppose an advanced type system allows the programmer to specify what operations are legit or not across different types.


One could imagine systems for that, but units of measure doesn't do any customization of different operations. It's just unit checking for for arithmetic, like in high-school math.




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

Search: