4 ms·
One of the things I really like about f# is thar you can tag even raw numbers with types. And even multiply/divide on those types.https://learn.microsoft.com/en
by Akronymus 4y ago
One of the things I really like about f# is thar you can tag even raw numbers with types. And even multiply/divide on those types.https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...
- fbdab103 4y agoNim as well, they even have an example[0] with currencies. [0] https://nim-lang.org/docs/manual.html#distinct-type-modeling-currencies https://nim-lang.org/docs/manual.html#distinct-type-modeling...
- cb321 4y agoOne can, of course, go much further than simply distinct number types in Nim: https://github.com/ringabout/awesome-nim#science https://github.com/ringabout/awesome-nim#science (Unchained seems maybe the most featureful of those units packages.)
- zdragnar 4y agoThis is probably the one feature from F# that I truly miss in other languages.
- aembleton 4y agoKotlin 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 https://kotlinlang.org/docs/type-aliases.html
- dragonwriter 4y agoYes, 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).
- aembleton 4y agoGood point, it doesn't solve the problem
- Akronymus 4y agoThat'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.