Skip to content

Instantly share code, notes, and snippets.

@soc
Created September 25, 2011 19:14
Show Gist options
  • Save soc/1240991 to your computer and use it in GitHub Desktop.
Save soc/1240991 to your computer and use it in GitHub Desktop.
Thoughts about Units of Measurement
Units of Measurements
Features:
- Computations: 4s + 5s == 9s
- Conversions
between dimensions: 5m + 10cm == ...
between metric systems: 5ft + 10cm = ...
- Dimensions: 5m * 5m == 25m² || 10m/s * 1s == 10m || 10m² / 5m == 5m
- Everything above combined!
Requirements:
- Static type safety of computations and conversions
- Verification at compile-time
- No performance penalty at runtime
- No restriction on numeric types
Problems:
- How to express this in an extendable, user-friendly way?
- Large signatures
- Heavy boilerplate definitions
- ...
Questions:
- Is this possible without language extensions?
- Are there any sacrifices possible without substantially reducing the usefulness?
- If this is implemented via some compile-time-only construct (like Phantom Types), how will it compare to specialization?
- If it is not possible without runtime wrapping, how much performance penalty will we get with something like Measurement[Length[Meter[Numeric[T]]]]?
- Code generation necessary?
Discussion:
- Hard problem, with many uncompromisable requirements
- Currently no support in Java's or Scala's Development Kit
- Having core classes covering common ground is always desirable
- consider Date&Time libraries as a potential consumer
- consider Groups, Semigroups, Monoids as another area not covered in the SDK
- Java failed twice (JSR-108/JSR-275) with a "library-only" approach
- Scala's type system is much more powerful, but is it enough?
- F# has special support build into the language, but is this approach acceptable for general-purpose language like Scala?
- IMHO some more generic support benefiting other areas would be desirable, e. g. Numeric[T]
- Is it possible to ship with a feature set covering 99,9% of all user's requirements, making the issue of extendability much less pressing?
- If the ugliness is only at the declaration site it is much more acceptable
References:
- ScalaU and earlier work, especially the old units compiler plugin
- F#: Language support for Units of Measurement
- Frink: Special purpose language for unit calculations
- Java: Failed standardization requests JSR-108/JSR-275
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment