aradi / fypp

Python powered Fortran preprocessor
http://fypp.readthedocs.io
BSD 2-Clause "Simplified" License
192 stars 30 forks source link

Constructing string of list with proper ending #5

Closed xinzhang8noaa closed 4 years ago

xinzhang8noaa commented 4 years ago

Hi, I am trying to use the fypp to generate following code:

.. code-block:: bash

  procedure, pass :: get_real_vector_copy
  procedure, pass :: get_real_vector_ref
  procedure, pass :: get_integer_vector_copy
  procedure, pass :: get_integer_vector_ref

  generic         :: get  => &
                     get_real_vector_copy, &
                     get_real_vector_ref, &
                     get_integer_vector_copy, &
                     get_integer_vector_ref  , &

Here is my code with fypp directives:

 #:set TYPE_KINDS = [ ( 'real', 'RP'),               ( 'integer', 'IP')]
 #:set VERSIONS   = [ ( 'copy', 'allocatable', '='), ( 'ref', 'pointer', '=>')]

 #:for TYPE, KIND in TYPE_KINDS
 #:for VER, _, _ in VERSIONS
     procedure, pass :: get_${TYPE}$_vector_${VER}$
 #:endfor
 #:endfor

      generic         :: get  => &
  #:for TYPE, KIND in TYPE_KINDS
  #:for VER, _, _ in VERSIONS
                     get_${TYPE}$_vector_${VER}$, &
  #:endfor
  #:endfor

my issue is how to remove the last , & in generic statement.

Thanks,

aradi commented 4 years ago

My suggestion would be to use Fortrans capability of extending a generic defintion with subsequent 'generic =>' directives:

#:for TYPE, KIND in TYPE_KINDS
  #:for VER, _, _ in VERSIONS
    generic :: get  => get_${TYPE}$_vector_${VER}$
  #:endfor
#:endfor

Theoretically, it is also possible to achieve with fypp exactly what you wish, but that would require an additional macro call and would make the code much less readable.

xinzhang8noaa commented 4 years ago

Many Thanks ! your suggestion works !

I like fypp !