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

Kotlin has type aliases. This means you can write something like:

  typealias Cent = Int
and then use a type Cent just like you can an Int, with multiplication and everything else. More info: https://kotlinlang.org/docs/type-aliases.html


Yes, you can, but you shouldn’t to solve this problem, because typealiases aren’t new types, just–as the name suggests–aliases, so anything aliased to Int is interchangeable with Int and everything else aliased that way, so you still have exactly the same problem.

Also, cents * cents shouldn’t be cents (though cents * unitless ints should be).


Good point, it doesn't solve the problem


That's not quite the same thing, as in f# a unit of measure actually makes not interchangable. While a type alias, afaik, literally is just an alias, without preventing you to do something like that:

    typealias Cent = Int
    typealias Euro = Int

    SomeEuro = SomeCent
In f#, measures aren't bound to any specific numeric type either.




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

Search: