Issue with printing entries of the dictionary.
arunprasaad2711 opened this issue · 3 comments
Hello!
I wrote a small test program to create a dictionary. However, I am not able to print the dictionary or the key values in the dictionary.
This is the sample program
use dictionary implicit none type(dict) :: sample_dict1 integer :: nx = 101, ny = 201, nt = 10001 sample_dict1 = ('nx'.kv.nx) // ('ny'.kv.ny) // ('nt'.kv.nt) print *, sample_dict1
You cannot print a derived type in fortran. There is no possible overloading of a write statement.
However, you can print a dictionary:
type(dict) :: d
call print(d)
You cannot print a derived type in fortran. There is no possible overloading of a write statement.
I don't think that's strictly correct with Modern Fortran. What about derived type IO? See Modern Fortran Explained, Section "17.2 Non-default derived-type input/output"
You can have TBPs overloading IO for derived types:
generic :: read(formatted) => r1, r2
generic :: read(unformatted) => r3, r4, r5
generic :: write(formatted) => w1
generic :: write(unformatted) => w2, w3
e.g.:
program
use person_module
integer id, members
type (person) :: chairman
:
write (6, fmt="(i2, dt(15,6), i5)" ) id, chairman, members
! This writes a record with four fields, with lengths 2, 15, 6, 5,
! respectively
end program
module person_module
type :: person
character (len=20) :: name
integer :: age
contains
procedure :: pwf
generic :: write(formatted) => pwf
end type person
contains
subroutine pwf (dtv, unit, iotype, vlist, iostat, iomsg)
! Arguments
class(person), intent(in) :: dtv
integer, intent(in) :: unit
character (len=*), intent(in) :: iotype
integer, intent(in) :: vlist(:)
! vlist(1) and (2) are to be used as the field widths
! of the two components of the derived type variable.
integer, intent(out) :: iostat
character (len=*), intent(inout) :: iomsg
! Local variable
character (len=9) :: pfmt
! Set up the format to be used for output
write (pfmt, ’(a,i2,a,i2,a)’ ) &
’(a’, vlist(1), ’,i’, vlist(2), ’)’
! Now the child output statement
write (unit, fmt=pfmt, iostat=iostat) dtv%name, dtv%age
end subroutine pwf
end module person_module
Yes, but in modern fortran fdict
has less importance (everything can be handled with classes). It is primarily a f90 module and in f90 (f95) this is not possible.
Thanks for the elaborate example though. :)