If a derived type contains private
components, it is impossible to use structure constructor in assignment. The standard requires that the constructor includes values for all components with the exception of those having default initializer or some complicated inheritance (C7100 (R756)). Obviously, it also requires that the component is accessible (C7103).
code example
module typ
type point
real :: x,y
real, private :: angle
end type point
...
end module typ
program main
use typ
type (point) :: p
p = point(1.2,3.4)
print '(g0)', p%x
end program main
$ gfortran struct_constr.f90
struct_constr.f90:12:7:
12 | p = point(1.2,3.4)
| 1
Error: No initializer for component 'angle' given in the structure constructor at (1)
Would it really break anything if that rule were relaxed and did not apply to private components? After all, they are meant to be private, and as such, to be initialized and maintained through the module procedures only. On the other hand, assignment via single-component statements (object%component=value
) for a rich type seems an overkill.