`generic :: write (formatted)` routines in gfortran called by other derived types

I use several derived types. For debugging purposes, I would like to print them beautifully on the screen with generic :: write(formatted).

Here is an example program showing what I mean:

program main
  use :: test_mod
  implicit none

  type(test1) :: t1
  type(test2) :: t2

  t1 = test1(name="t1")
  t2 = test2(name="t2", t1)
  print *, 'Test1:', t1
  print *, 'Test2:', t2
end program

Here is the module:

module test_mod
  implicit none
  private
  public :: test1, test2

  type :: test1
    character(2) :: name
  contains
    procedure, private :: test1_writef
    generic :: write(formatted) => test1_writef
  end type test1

  type :: test2
    character(2) :: name
    type(test1) :: t1
  contains
    procedure, private :: test2_writef
    generic :: write(formatted) => test2_writef
  end type test2

contains

  subroutine test1_writef(self, unit, iotype, v_list, iostat, iomsg)
    class(test1), intent(in)    :: self      ! Object to write.
    integer, intent(in)         :: unit      ! Internal unit to write to.
    character(*), intent(in)    :: iotype    ! LISTDIRECTED or DTxxx
    integer, intent(in)         :: v_list(:) ! parameters from fmt spec.
    integer, intent(out)        :: iostat    ! non zero on error, etc.
    character(*), intent(inout) :: iomsg     ! define if iostat non zero.
    write (unit, "(a)", IOSTAT=iostat, IOMSG=iomsg) self%name
  end subroutine test1_writef

  subroutine test2_writef(self, unit, iotype, v_list, iostat, iomsg)
    class(test2), intent(in)    :: self      ! Object to write.
    integer, intent(in)         :: unit      ! Internal unit to write to.
    character(*), intent(in)    :: iotype    ! LISTDIRECTED or DTxxx
    integer, intent(in)         :: v_list(:) ! parameters from fmt spec.
    integer, intent(out)        :: iostat    ! non zero on error, etc.
    character(*), intent(inout) :: iomsg     ! define if iostat non zero.
    write (unit, "(a, ' <', a, '>')", IOSTAT=iostat, IOMSG=iomsg) self%name, self%t1
  end subroutine test2_writef

end module test_mod

The launch of this program I expected to get:

Test1: t1
Test2: t2 <t1>

But instead with gfortran I get:

Test1: t1
Test2: t2 <>

The string "t1" is not displayed on the screen .

I am using this version of gfortran:

[egissi@shibax ~]$ gfortran --version
GNU Fortran (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I know that I could just replace self%t1with self%t1%name, but my types are much more complicated than that, and I'd rather not repeat the same format in other derived types.

What am I missing? How can I get this behavior?

+4
source share
1

, , "" - . ,

print *, 'Test1:', t1
print *, 'Test2:', t2

test1_writef test2_writef t1 t2. .

write (unit, "(a, ' <', a, '>')", IOSTAT=iostat, IOMSG=iomsg) self%name, self%t1

("(a,' <',a,'>')"). self%name self%t1 . self%t1 test1_writef, dt:

write (unit, "(a, ' <', dt, '>')", IOSTAT=iostat, IOMSG=iomsg) self%name, self%t1
+2

Source: https://habr.com/ru/post/1695778/


All Articles