I would like to have compile time checks that array sizes are compatible.
For example the zip and transpose here are safe because a and b have the same length:
(a: Seq[Int]) => { val b=a.map(_+1); Seq(a,b).transpose }
Whereas this is unsafe because the code will die at runtime if a and b have different dimensions:
(a: Seq[Int], b: Seq[Int]) => Seq(a,b).transpose
In general if I give the dimensions of the arguments to some code, it is often possible to determine the dimensions of each variable inside the code and finally the result of the code. Sometimes such static analysis might show up bugs immediately, sometimes the static analysis might warn about some assumptions, such as an assumption that two files have the same length, assumptions that can and should be checked as soon as possible at runtime. This would make the code more reliable and so I would love such checks.
I have googled and found lots of discussions but no actual implementations.
I would like this to fail at compile time:
Seq(Seq(1,2,3),Seq(1,2)).transpose
I would like this to emit a compiler warning that this will fail if a is not of dimension [2]:
(a: Seq[Int]) => Seq(a, Seq(1,2)).transpose
I would like this (or something similar) to emit no compiler warnings:
(a: Seq[Int]) => a.length match {
case 2 => Seq(a, Seq(1,2)).transpose
case _ => throw new Exception(s"a has the wrong dimension")
}