Homepage

Jannis Teunissen

User Tools

Site Tools

Action disabled: source

Ideas for Fortran improvements

Default values for optional arguments

Currently, default values for optional arguments have to be handled like this:

subroutine test(n_arg)
  integer, intent(in), optional :: n_arg ! Default is 10
  integer :: n_used
 
  if (present(n_arg)) then
     n_used = n_arg
  else
     n_used = 10
  end if
  ... ! Do something with n_used

It would be more convenient if this was possible:

subroutine test(n)
  integer, intent(in), optional :: n = 10 ! Default is 10
  ...

Note that this syntax is also used to initialize variables with the 'save' attribute, but since that attribute does not apply to optional arguments it should be fine.

Multi-dimensional vector indexing

Currently, you can use vectors for indexing. Given three vectors a, b, c of equal length, you can e.g. do:

a(b(:)) = c

which is the same as writing

a(b(1)) = c(1)
a(b(2)) = c(2)
...

Now suppose we have a matrix D, and we have an index vector ix. Then there is currently now way to do

ix = [1, 1]
D(ix) = 0 ! Illegal

Instead, you have to write

ix = [1, 1]
D(ix(1), ix(2)) = 0 ! Correct

I suggest to add the following syntax rule: if a /n/-dimensional array is indexed by a single vector of length /n/, then automatically treat is as an index, so that both the above code snippets behave the same.

Passing arrays of unknown dimension

It would be great if something like this was supported:

subroutine example(my_array, ndim)
  integer, intent(in) :: ndim
  real, intent(in)    :: my_array(dim=ndim)
  integer             :: nx(ndim)
 
  nx = shape(my_array)
  ...
end subroutine example

Page Tools