j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

"select rank()" should be more permissive. #169

Open ghost opened 4 years ago

ghost commented 4 years ago

One of the new features in F2018 is assumed-rank arrays and "select rank()" construct.

the "select rank()" construct should be allowed to accept one or more scalar integer, like "select case()" example:

subroutine sub(a)
  integer :: a(..)
  select rank(a)
    rank(0)
      ...
    rank(1:3)
      ...
    rank(4:)
      ...
  end select
end subroutine sub

this will allow for more generic programming.

klausler commented 4 years ago

What would you do in the rank(1:3) block that would be correct and useful, yet couldn't be done with if (rank(a)>=1.and.rank(a)<=3) then ... end if?

ghost commented 4 years ago

An assumed-rank variable name must not appear in a designator or expression except as one of the following:

As you can see there are few operations that you can do with an assumed-rank array unless it is inside a "select rank()" construct. for example:

subroutine sub(a,b)
  integer :: a(..)
  ....
  a = 0.
  ...
end subroutine
klausler commented 4 years ago

Your if (a/=b) isn't going to do what you think it will, SELECT RANK or not.

ghost commented 4 years ago

Your if (a/=b) isn't going to do what you think it will, SELECT RANK or not.

I corrected the example.

klausler commented 4 years ago

Ok. But that kind of rank-independent programming would be just as useful outside a multiple-rank case of a SELECT RANK, if that feature were to exist.

ghost commented 4 years ago

It would be better if they remove those restrictions on assumed-rank arrays outside of select rank.

klausler commented 4 years ago

That's basically my point (or rather, absent more rank-independent features, the multi-rank RANK case doesn't do much good; and given more rank-independent features, the multi-rank RANK case isn't as valuable).

I can see a multi-rank RANK case being more useful today (absent more rank-independent features) if SELECT RANK could accept multiple assumed-rank arrays.

han190 commented 1 year ago

Addtionally, allowing integer index in select rank would be better

subroutine sub(a)
  integer :: a(..)
  integer :: i

  select rank(a)
  rank (i=1:4)
    print *, i
  end select
end subroutine sub

with the "Using integer arrays to specify the rank and bounds of an array", we could do something like

subroutine sub(a, b)
  integer, intent(in) :: a(..)
  integer, allocatable, intent(out) :: b(..)
  integer, allocatable :: shp(:)
  integer :: i, j

  select rank(a)
  rank (i=1:4)
    shp = [(j, j=1, i + 1)]
    allocate (b(shp)
  end select
end subroutine sub