Open perazz opened 1 year ago
Here is a corresponding fortran-lang Discourse thread that first introduced this issue: https://fortran-lang.discourse.group/t/allow-subarray-indexing-as-input-to-pure-and-elemental-functions/4048
Is this just new syntax for an implied DO loop in an array constructor?
Well, most of the syntax in any compiled languages is literally wrappers over pointers, gotos, and loops. So in that sense yes, I agree it's "just new syntax". IMHO it would be a step forward for the language UX. But maybe not. As a compiler developer, it would be great to have your opinion as to whether any better/further optimization would be possible (for example, whenever those array slices are passed by value instead of by reference).
I also think that would pave the way for modern loop constructors (such as partially covered in do concurrent
):
! Use array slicing syntax
do I = -32:18:4
end do
! Use array
do I = [-3,425,9999]
end do
! Use array
do I = my_array
end do
It would be useful if
elemental
pure
functions could be used with subarray indexing inputs. I think that could have two major benefits:value
to the function, hence save memory / allow faster code execution.Imagine something like:
Now, if you want several powers you need to do
While with subarray-index-enabled functions, it would be simply
Simple, optimizeable, readable, concise. I would use that a lot.
The simplest usage would be to return an array with those same indices
which could be used like:
I guess the same restrictions as to working with elemental functions would apply, that probably wouldn’t be hard to implement in current compilers. Thoughts?