Declaring *assumed size* array with constant constructor

Fortran allows constant arrays to be declared with assumed-size syntax and constant expression constructor, like

integer, parameter :: tmpa(*) = [0,1,2,3,4,5]

Why couldn’t it be a non constant array? The following workaround does the job:

integer, parameter :: tmpa(*) = [0,1,2,3,4,5]
integer :: arr(size(tmpa))=tmpa

So why not just allow

integer :: arr(*)=[0,1,2,3,4,5]

You’d then run into the issue that arr could be a dummy argument, where assumed-size is an existing feature. It would be messy to have to have to assign a different meaning to (*) depending on whether arr was a dummy argument or not. There was no such issue with parameter.

1 Like

It seems, however, that a dummy argument specification in a procedure cannot have an initializer. I am not sure whether it is standard-forbidden but both gfortran and ifort/ifx report an error, not only on an array, but also on scalar dummy.
So, I think, that would not be a serious issue. Also, C has always allowed such a declaration, as in int arr[ ]={0,1,2,3,4,5};

I’m not saying it would be ambiguous, but it would be complicated and maybe confusing to define, for something that can already be done another way.

From time to time, I catch myself trying to do something like that, only to realize it doesn’t work… and then I remember I tried the same in the past - but forgot it just won’t work in the meantime. :laughing:
I guess the fact I tried this more than once over the years means it’s somewhat logical for a user to expect this is valid. I’m sure it’s not just me and @msz59 who tried something similar. However I think @sblionel 's explanation why this doesn’t work makes sense. Also, the fact C does indeed allow something like that with its “arrays” doesn’t mean much, if anything.

Edit: the syntax integer, parameter :: tab(*) = [0,1,2,3] is called implied-shape, not assumed-size. So it would be not only unambiguous but quite a different feature. The asterisk * serves so many purposes in Fortran :slight_smile: