Fortran needs to be extended for this. I was thinking something like this:
function f(x)
dim :: n, m
real, intent(in) :: x(n,m)
real :: f(n,m)
Which has many advantages, consider a matmul, currently:
function matmul(A, B) result(C)
real, intent(in) :: A(:,:), B(:,:)
real :: C(size(A,1),size(B,2))
Note that you can’t use shape
easily for this and note that dimensions are not tied, so the compiler cannot check that A and B are conforming. Now after the extension:
function matmul(A, B) result(C)
dim :: m, n, k
real, intent(in) :: A(m,n), B(n,k)
real :: C(m, k)
The dim
behaves just like A(:)
, in other words, the array still gets passed in with a descriptor. But you can now tie the different dimensions together and the compiler can check (at compile time and/or runtime) that the arrays A, and B are conforming with their dimensions, so you do not need to do this check by hand inside the subroutine. And it is consistent if you want to use the old fashioned arrays:
function matmul(m, n, k, A, B) result(C)
integer, intent(in) :: m, n, k
real, intent(in) :: A(m,n), B(n,k)
real :: C(m, k)
Note 1: instead of dim :: m, n, k
, we could use integer, infer :: m, n, k
, to be consistent with integer, intent(in) :: m, n, k
.
Note 2: real, intent(in) :: A(:,:), B(:,:)
would be equivalent to integer, infer :: m, n, k, l; real, intent(in) :: A(m, n), B(k,l)