ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - kunalsheth/units-of-measure: Type-safe dimensional analysis and unit conversion in Kotlin.
Type-safe dimensional analysis and unit conversion in Kotlin. - kunalsheth/units-of-measure
Visit Site

GitHub - kunalsheth/units-of-measure: Type-safe dimensional analysis and unit conversion in Kotlin.

GitHub - kunalsheth/units-of-measure: Type-safe dimensional analysis and unit conversion in Kotlin.

units-of-measure

Type-safe dimensional analysis and unit conversion in Kotlin.

demo image


Project Status

No longer maintained.

Building UOMs for the first time is quite slow on the latest Kotlin compiler versions (1.3.70+). To improve compile times, UOMs 7.0.0+ will be repackaged as an Λrrow Meta plugin.

Usage

Take a look at the project website for installation and usage: http://units.kunalsheth.info

You can also take a look at this sample project for a complete gradle setup and to learn about some of the more advanced features.


More sample code:

val mass1 = 3.kilo(Gram)
val mass2 = 14.Ounce
val sum = mass1 + mass2
// mass1 + 3.Days // will not compile

assert(sum in 7.5.Pound `±` 1.Ounce)
assert(sum in 3.3.kilo(Gram)..7.5.Pound) // this works too
// assert(sum in 7.4.Kilowatts..7.5.Pounds) // will not compile

val ratio = 2.Foot / 1.Metre
assert(ratio in 60.Percent `±` 5.Percent)
assert(ratio.Percent.toInt() in 55..65)


assert(1.kilo(Gram) == 1000.Gram)
assert(10.milli(Metre) == 1.centi(Metre))
assert(60000.milli(Second) == 1.Minute)


assert(420.Degree % 1.Turn in 60.Degree `±` 1.Degree)


val speed = 65.Mile / Hour
val time = 27.Minute
val distance = speed * time
val aBitFaster = distance / (time - 30.Second)

assert(distance == time * speed)
assert(distance in 29.Mile..30.Mile)
assert(distance in 30.Mile..29.Mile) // this works too
assert(aBitFaster in speed..(speed + 4.kilo(Metre) / Hour))


val threshold = 0.001.Foot / Second / Second
sequenceOf(0, 1, 4, 9, 16, 25).map { it.Foot }
        .derivative(::p)
        .derivative(::p)
        .zipWithNext { a, b -> a in b `±` threshold }
        .forEach { assert(it) }
// support for generic programming
fun <Q : Quan<Q>, DQDT : Quan<DQDT>> Sequence<Q>.derivative(p: (Q, `÷`, T) -> DQDT) = timeSeq()
        .zip(this)
        .zipWithNext { (x1, y1), (x2, y2) -> p(
                (y1 - y2), `÷`, (x1 - x2)
        ) }

Background

Type-safe dimensional analysis and unit conversion can be extremely beneficial to a team. From personal experience, using type-safe calculations result in:

  • Faster Development — IDE autocomplete provides meaningful predictions, rather than just listing every number in scope.
  • Cleaner Code — Variable names will be of a reasonable length now that unit information is documented by the type.
  • Higher Confidence — All unit/dimension related bugs will show up at compile time. Debugging is less difficult and time-consuming.

units-of-measure's novel, metaprogramming approach to the problem makes it:

  1. Incredibly Extendable — Adding new functionality is as simple as adding a line to your build file. No tedious "hand-coding" is required.
  2. Small — You only generate what you need. You are not forced to bundle every conceivable unit, quantity, and dimension with your app.
  3. Bug Resistant — Programming by hand is error prone and time-consuming. Code generation can ensure correctness.

Todo List

  • Make it work.
  • Generate implicit relationships as well.
  • Make annotations easier to write and manage.
  • Add support for unit conversions.
  • Add docs. (http://units.kunalsheth.info)
  • Add metric prefixes.
  • Multiplatform.
  • Stronger support for generic use (Quantity<This, IntegralOfThis, DerivativeOfThis>)
  • * and ÷ singleton types for even safer proof-passing.
  • Publish on Gradle Plugin Portal.
  • Document serialization functionality.
  • Optimize for faster compilation and runtime.
  • Benchmark performance hit in contrast to primitives. (Can someone help me with this?)

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory