Re:Microfeatures I'd like to see in more languages
by ignormies on Friday January 06, 2023 @02:41AM
(#34272016)
I see this in some Rust libraries, as you can `impl` for built-in, primitive types.
For instance, the `embedded_time` library lets you do
200.microseconds()
5.Hz()
https://docs.rs/embedded-time/latest/embedded_time/
Re:Microfeatures I'd like to see in more languages
by int_19h on Friday January 06, 2023 @02:55AM
(#34272114)
They can be implemented as a library C++ thanks to templates and user-defined literals, and there's a proposal to add them to stdlib based on one of the existing implementations: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p19...
Re:Microfeatures I'd like to see in more languages
by kwhitefoot on Friday January 06, 2023 @04:05AM
(#34272593)
You can implement it in C# too with zero run time cost.
Re:Microfeatures I'd like to see in more languages
by int_19h on Friday January 06, 2023 @11:42PM
(#34285275)
I don't see how, except for manually rolling a struct for every new combination of units. The C# generic system is not flexible enough to define things like "m / s / s = (m/s^2)".
Re:Microfeatures I'd like to see in more languages
by brabel on Friday January 06, 2023 @06:40AM
(#34273634)
There's a Java compiler extension, Manifold[1], that adds this to Java.
[1] https://github.com/manifold-systems/manifold/tree/master/man...
Re:Microfeatures I'd like to see in more languages
by perrygeo on Friday January 06, 2023 @10:21AM
(#34275601)
Frink (which OP mentions for its datetime syntax) has the concept of units and unit conversions built into the core language - but that's about all it does! I'd love to have this feature in a general-purpose language.
Re:Microfeatures I'd like to see in more languages
by dima_vm on Friday January 06, 2023 @06:34PM
(#34282926)
You can make that in Kotlin, e.g.:
`3.mph` or `(-45..45).deg` for ranges.
with definition like:
val Double.mph: Speed
get() = Speed.mph(this)
data class Speed(val ms: Double)
companion object {
fun mph(mph: Double) = Speed(mph * 1609.344 / 3600.0)
}
Re:Microfeatures I'd like to see in more languages
by still_grokking on Sunday January 08, 2023 @05:19PM
(#34303858)
AFAIK F# has these and that's about it