j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
175 stars 14 forks source link

Extending associate construct to improve readability in object oriented contexts #319

Open francescosalvadore opened 8 months ago

francescosalvadore commented 8 months ago

Extending associate construct for object oriented contexts would be very useful for readability and development in my opinion.

Instead of writing something like this:

associate(nx => self%nx, ny => self%ny, nz => self%nz, ng => self%ng, &
          dcsidx_gpu => self%base_gpu%dcsidx_gpu, detady_gpu => self%base_gpu%detady_gpu,   &
          dzitdz_gpu => self%base_gpu%dzitdz_gpu, dcsidxs_gpu => self%base_gpu%dcsidxs_gpu, &
          detadys_gpu => self%base_gpu%detadys_gpu, dzitdzs_gpu => self%base_gpu%dzitdzs_gpu, &
          indx_cp_l => self%equation_base%indx_cp_l, &
          indx_cp_r => self%equation_base%indx_cp_r, &
          cp => self%equation_base%calorically_perfect, &
          t0 => self%equation_base%t0, channel_case => self%equation_base%channel_case, &
          enable_ibm => self%equation_base%enable_ibm)

I would like to write:

associate(from self: nx, ny, nz, ng; &
          from self%base_gpu: dcsidx_gpu, detady_gpu, dzitdz_gpu, dcsidxs_gpu, detadys_gpu, dzitdzs_gpu; &
          from self%equation_base: indx_cp_l, indx_cp_r, cp => calorically_perfect, t0, enable_ibm)

Francesco

PierUgit commented 7 months ago

This use case is very similar to the one of the Pascal with statement.

To be even more compact, it could have a "all" specifier, saying the all the components shall be associated:

associate (from self:(all); from self%base_gpu:(all); from self%equation_base:(all))

To go further, it could be combined with #321 :

associate (from self:(all)) &
          (from base_gpu:(all); from equation_base:(all)) &
          (cp => calorically_perfect)

Additionnaly, a suffix specifier could solve the case where one wants to do that with several objects of the same type:

associate (from object1:(all); from object2:(all,suffix="2"))

would be equivalent to

associate (nx => object1%nx, [...,] nx2 => object2%nx )