# Private type allowed in a submodule?

**URL:** <https://fortran-lang.discourse.group/t/private-type-allowed-in-a-submodule/9226>\
**Category:** Help\
**Created:** [February 18, 2025, 10:36pm UTC](https://fortran-lang.discourse.group/t/private-type-allowed-in-a-submodule/9226 "2025-02-18T22:36:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [February 18, 2025, 10:36pm UTC](https://fortran-lang.discourse.group/t/private-type-allowed-in-a-submodule/9226/1 "2025-02-18T22:36:42Z")

</div>

This program was inspired by a recent discussion in the Intel Fortran forum. It attempts to declare a private type in a submodule.

```auto
module points
   interface
      module function point_dist(p1, p2) result(distance)
        real,dimension(2),intent(in) :: p1, p2
        real :: distance
      end function point_dist
   end interface
 end module points
 
 submodule (points) points_a
   type, private :: point
      real :: x, y
   end type point
 contains
   module function point_dist(p1,p2) result(distance)
     real,dimension(2),intent(in) :: p1,p2
     real :: distance
     type(point) :: a, b
     a = point(p1(1),p1(2))
     b = point(p2(1),p2(2))
     distance = sqrt((a%x - b%x)**2 + (a%y - b%y)**2)
   end function point_dist
 end submodule points_a
 
 Program Test_program
   
   Use points
   Implicit none
   real, dimension(2):: p1 = [10,20], p2 = [20,10]
   real dist
   dist = point_dist(p1, p2)
   
   write(*,*)dist
   
 End program Test_program

```

Gfortran won’t compile it. Its first error message is

```auto
   11 | type, private :: point
      | 1
Error: Derived type at (1) can only be PRIVATE in the specification part of a module

```

If the 3 lines declaring the type point are moved from the beginning of the submodule to the beginning of its parent module then gfortran happily compiles and runs the program. My question: is a private type declaration forbidden in a submodule?

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [February 19, 2025, 1:10am UTC](https://fortran-lang.discourse.group/t/private-type-allowed-in-a-submodule/9226/2 "2025-02-19T01:10:37Z")

</div>

My comment does not answer your question. Best person would likely be `@sblionel`. Adding to the discussion, the attribute `private` would mean the entity is inaccessible outside the submodule. But isn’t it already inaccessible when placed in the submodule? Isn’t it, therefore, redundant to have the `private` attribute? I wish there was a way to access submodule declarations.

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [February 19, 2025, 2:32am UTC](https://fortran-lang.discourse.group/t/private-type-allowed-in-a-submodule/9226/3 "2025-02-19T02:32:58Z")

</div>

Extending @shahmoradi 's post a little bit:

In Fortran, the `public`|`private`|`protected` attributes **belong** to the `module`, so they cannot be used in a `submodule`, `subprogram` or `main-program`.

Additionally, a submodule has access to everything in the `ancestor-module` but anything declared in a submodule is private and can only be seen by its descendants, unless it’s a `separate-module-subprogram` —i.e., if it has its interface declared in the `ancestor-module` or in a `parent-submodule`, with `module` as a prefix.
