# Structure constructors for derived type with private components

**URL:** <https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394>\
**Category:** Language enhancement\
**Created:** [December 12, 2021, 5:11pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394 "2021-12-12T17:11:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 12, 2021, 5:11pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/1 "2021-12-12T17:11:21Z")

</div>

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**
>
> ```auto
> 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
> 
> ```
> 
> ```nohighlight
> $ 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.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 12, 2021, 7:42pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/2 "2021-12-12T19:42:53Z")

</div>

See my reply to Normal Kirby’s comment in [Doctor Fortran in “Not My TYPE” - Doctor Fortran (stevelionel.com)](https://stevelionel.com/drfortran/2020/06/30/doctor-fortran-in-not-my-type/). I would expect the use of a constructor routine for a type with private components. It would look just like a constructor. In some cases, it may work to provide default initializers for those components.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 12, 2021, 9:12pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/3 "2021-12-12T21:12:05Z")

</div>

Actually I read your post and the discussion before I started this topic. I just thought that the requirements of the standard here are a bit too stringent.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 12, 2021, 9:38pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/4 "2021-12-12T21:38:37Z")

</div>

The idea of a structure constructor is that it constructs a complete value of that type. If the type owner wants a user to be able to use constructor syntax, they can export their own constructor routine.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [December 12, 2021, 9:42pm UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/5 "2021-12-12T21:42:54Z")

</div>

I understand that. But on the other hand, there already are exceptions. If the type contains an allocatable array component, it is _legally_ omitted in the constructor. And it is OK, standard conforming.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 13, 2021, 12:40am UTC](https://fortran-lang.discourse.group/t/structure-constructors-for-derived-type-with-private-components/2394/6 "2021-12-13T00:40:46Z")

</div>

That’s fine. Allocatable arrays always have a defined status, either allocated or unallocated.
