Lately I've been playing around with static dimensional analysis in Rust. The overall idea is similar: Use PhantomData to add a type parameter and define an empty struct for each unit. So you might end up with, say, Scalar<Newtons> or Vec3<Meters>.<p>Dividing and multiplying units statically is where I've had trouble so far. I think I've found a way, but it would depend on negative trait bounds, as discussed here:<p><a href="https://github.com/rust-lang/rfcs/issues/1053" rel="nofollow">https://github.com/rust-lang/rfcs/issues/1053</a><p>Ideally, I'd like to be able to do something like this:<p><pre><code> let a: Scalar<Joules> = Scalar::new(2.0);
let b: Scalar<Seconds> = Scalar::new(3.0);
let c: Scalar<Watts> = a / b;
// Watts is a type synonym for Over<Joules, Seconds>.
// Other derived units would use Times<U, V>. E.g.:
type Pascals = Times<Newton, Times<Meter, Meter>>.</code></pre>
For an example of a similar technique in Nim: <a href="https://github.com/unicredit/linear-algebra/" rel="nofollow">https://github.com/unicredit/linear-algebra/</a><p>If I understand correctly the author remark about the lack of Rust support for value parameters, it seems that it should be equivalent to the Nim feature (static[int]) that has allowed me to write type-checked matrix operations there
Ok this is seriously amazing (coming from a C++ guy). However, one thing I don't like with putting matrix dimensions in templates is that then you can't construct them at runtime. I do understand the obvious - that you can't have both static type checks on all operations and runtime-determined matrix sizes. Though I would kill for a language which would specialise my code at runtime and throw an exception for compilation errors. So you could write, e.g.<p><pre><code> template <int m, int n, int l>
Matrix<m, l>
mul(Matrix<m, n> lhs, Matrix <n, l> rhs)
{...impl...}
</code></pre>
And then be able to call it like<p><pre><code> int m, n, k, l = ... read from file or whatever
Matrix <m, n> m1 = ...;
Matrix <k, l> m2 = ...;
try {
Matrix <int o, int p> m3 = m1 * m2;
// ^ code compiled dynamically
// or loaded from cache, based on
// runtime types of m1 and m2.
// o and p set to the result
// of type inference.
// I could imagine even having
// specialised versions with inline
// assembly for specific dimensions.
} catch (DynamicCompilationException e) {
print("dimensions not compatible");
}
</code></pre>
Java could be it, if it had reified generics. You'd create an implementation of Num, or load one from cache, then instantiate the template and attempt to call the mul function.<p>Or you could abuse the invoke dynamic feature - create specialised functions matrixMultiply$m$n$l and classes Matrix$m$n from some other templating language as needed, then do an invoke dynamic based on type. But this would be very cumbersome to use, I think.
For assorted type system/PhantomData madness, see also:<p><a href="http://maniagnosis.crsr.net/2015/07/abstracted-algebra-in-rust.html" rel="nofollow">http://maniagnosis.crsr.net/2015/07/abstracted-algebra-in-ru...</a><p><a href="http://maniagnosis.crsr.net/2015/07/more-abstracted-algebra-in-rust.html" rel="nofollow">http://maniagnosis.crsr.net/2015/07/more-abstracted-algebra-...</a>