How can define a function that accepts colon `a:b` inputs for integer ranges

For example I want to write code like

type :: thing
! data here
contains
procedure :: f => thing_f
end type

elemental subroutine thing_f(obj, index)
class(thing), intent(in) :: obj
integer ::  index
! code here
end subroutine

type(thing) :: obj
call obj%f(1:10)    ! error #6200: A colon (:) is not valid in this context.

I can however do the following without an error

call obj%f([1:10])

So an array input isn’t the trick I am looking for. Is it possible to specify a a:b type of argument for a function?

No, not directly at least using the standard language facilities. See this comment at another thread. As Fortranners seek further abstraction in their codes, having such a flexibility will be useful and it will be good if a future Generics capability builds on it but that is all is quite a while away.

In the mean time, if you share what you’re trying to achieve, readers may be able to suggest other possible workarounds.

I think that’s not possible, but you can pass the a and b numbers separately to your subroutine, and inside do a:b to index an array.

2 Likes