Hacker News new | past | comments | ask | show | jobs | submit login

> Rails prides itself on being able to write `5.minutes` - they monkey patch numbers to add a minutes function.

That can be done in strongly, statically typed languages with either extension functions (Kotlin, Dart) or type classes (Rust, Haskell).

People do it because there are advantages beyond the subjective reason that it reads better (I can speak several human languages and in all of them you would say number-timeUnit - so it's not just English even if it's not completely universal). It's also easier to "look up" functions based on the type of the value, for example.




Don't forget F#'s units of measure! Which by the way are pretty cool, as the compiler automatically infers new units of measure for you:

    > [<Measure>] type km;
    > [<Measure>] type hour;
    > let distance = 60.0<km>;
    > let time = 0.5<hour>;
    > let avgSpeed = distance / time ;;
    val avgSpeed : float<km/hour> = 100.0
(And of course forbids physically illegal stuff such as adding values with different dimensions)




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: