Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnamed array #307

Open
Sandwich-X opened this issue Jul 10, 2023 · 5 comments
Open

Unnamed array #307

Sandwich-X opened this issue Jul 10, 2023 · 5 comments

Comments

@Sandwich-X
Copy link

Analogously to
PRINT *, "abcdef"(3:3)
it should be possible to have
PRINT *, ["a","b","c","d","e","f"](3)
or
PRINT *, [3,1,4,1,5,9](3)
and similar.

@FortranFan
Copy link
Member

FortranFan commented Jul 10, 2023

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!!

@certik
Copy link
Member

certik commented Jul 10, 2023

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.

@Sandwich-X
Copy link
Author

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.

@certik
Copy link
Member

certik commented Jul 11, 2023

@Sandwich-X, good points. Would you allow it for arrays from functions, or only constant arrays like [1, 2, 3](j)?

@Sandwich-X
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants