Open Sandwich-X opened 11 months ago
Look into ASSOCIATE
construct: the language standard allows:
associate ( s => "abcdef") ; print *, s(3:3 ) ; end associate
associate ( s => ["a","b","c","d","e","f"] ) ; print *, s(3) ; end associate
associate ( n => [3,1,4,1,5,9] ) ; print *, n(3) ; end associate
end
C:\temp>gfortran -ffree-form p.f -o p.exe
C:\temp>p.exe
c
c
4
So you can propose either a shortened version, say ASSOCIATE
statement.
Or, if you seek even more compactness in the Fortran language, you can reach back to your original post where you propose even that be bypassed!!
Also a function that returns an array:
print *, f()(2)
Or any array expression like:
print *, (a+b)(2)
It becomes quite less readable quickly. The associate
construct, while more verbose, is reasonably readable, it's clear what it is doing I think.
Thanks for your comments!
Well, "abcdef"(3:3)
is also "allowed" already now.
It might be used in situations where a (constant) string appears only once in the whole source-code, so you can avoid to declare it as variable (or better constant via PARAMETER), e.g.:
DO i = 1, LEN_TRIM(string) ! here some declared variable string
c = string(i:i)
DO j = 1, 26
IF (c=="abcdefghijklmnopqrstuvwxyz"(j:j)) THEN ! here unnamed (constant) string
c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"(j:j) ! here unnamed (constant) string
EXIT ! j
END IF
END DO ! j
END DO ! i
(There might be better solutions for upper-case-ing a string, but that's not the point here.)
So, due to pure analogy to that, I would like to have the same possibility for (constant) arrays, which appear only once in the whole source-code
DO j = 1, 10
IF (n==[3,1,4,1,5,9,2,6,5,3](j)) THEN ! unnamed (constant) array
n = [2,7,1,8,2,8,1,8,2,8](j)) ! unnamed (constant) array
Of course, if the size of the unnamed string/array (which therefore is not "absolutely constant") changes in the next version of the software, you have to change also the j_end
(26/10), which could be avoided, if you use named variables and LEN_TRIM() / SIZE(), but this applies for both (string and array).
Probably internally (from the point of view of the compiler) strings and arrays might be pretty different objects/structures, but from the point of view of the user they are both bunches, lists, collections, ... of single objects of the same type, so I would like to be able to do the "same thing" with/to them.
Giving them an intermediate name via ASSOCIATE
is not what I want and honestly said I don't find it more readable.
@Sandwich-X, good points. Would you allow it for arrays from functions, or only constant arrays like [1, 2, 3](j)
?
Honestly said, I haven't thought about that yet. So, I guess, for the beginning I would be happy if it works for constant arrays (although - again honestly said - I have had only one case until now, where I really could have needed it) and maybe, if it turns out, that it might be also usefull for functions, it could get extended.
Analogously to
PRINT *, "abcdef"(3:3)
it should be possible to havePRINT *, ["a","b","c","d","e","f"](3)
orPRINT *, [3,1,4,1,5,9](3)
and similar.