This shows you the differences between two versions of the page.
— | blog:fortran_improvements [2018/04/25 12:46] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Ideas for Fortran improvements ===== | ||
+ | |||
+ | ==== Default values for optional arguments ==== | ||
+ | |||
+ | Currently, default values for optional arguments have to be handled like this: | ||
+ | |||
+ | <code fortran> | ||
+ | subroutine test(n_arg) | ||
+ | integer, intent(in), optional :: n_arg ! Default is 10 | ||
+ | integer :: n_used | ||
+ | |||
+ | if (present(n_arg)) then | ||
+ | | ||
+ | else | ||
+ | | ||
+ | end if | ||
+ | ... ! Do something with n_used | ||
+ | </ | ||
+ | |||
+ | It would be more convenient if this was possible: | ||
+ | <code fortran> | ||
+ | subroutine test(n) | ||
+ | integer, intent(in), optional :: n = 10 ! Default is 10 | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | Note that this syntax is also used to initialize variables with the ' | ||
+ | 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: | ||
+ | <code fortran> | ||
+ | a(b(:)) = c | ||
+ | </ | ||
+ | which is the same as writing | ||
+ | <code fortran> | ||
+ | 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 | ||
+ | |||
+ | <code fortran> | ||
+ | ix = [1, 1] | ||
+ | D(ix) = 0 ! Illegal | ||
+ | </ | ||
+ | |||
+ | Instead, you have to write | ||
+ | <code fortran> | ||
+ | ix = [1, 1] | ||
+ | D(ix(1), ix(2)) = 0 ! Correct | ||
+ | </ | ||
+ | |||
+ | I suggest to add the following syntax rule: if a / | ||
+ | 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: | ||
+ | |||
+ | <code fortran> | ||
+ | subroutine example(my_array, | ||
+ | integer, intent(in) :: ndim | ||
+ | real, intent(in) | ||
+ | integer | ||
+ | |||
+ | nx = shape(my_array) | ||
+ | ... | ||
+ | end subroutine example | ||
+ | </ | ||