# Compilation time vs. C++

**URL:** <https://fortran-lang.discourse.group/t/compilation-time-vs-c/2430>\
**Category:** Help\
**Created:** [December 21, 2021, 12:35am UTC](https://fortran-lang.discourse.group/t/compilation-time-vs-c/2430 "2021-12-21T00:35:44Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [December 22, 2021, 10:32am UTC](https://fortran-lang.discourse.group/t/compilation-time-vs-c/2430/9 "2021-12-22T10:32:45Z")

</div>

> [@certik](#):
>
> In fact gfortran + cmake already fix this. You can try it yourself, just add a print statement to some function in a module, and dependent modules will _not_ be recompiled. So this is fixed in practice.

That’s a major win for gfortran 👍 I really hope Intel would start doing this as well!

> [@martin](#):
>
> However, any private method must be included in the class definition in the module interface. Any derived-type from these internal modules which appear as arguments in these private methods are now exposed and the compilation dependency chain is not broken anymore, as was originally intended.

Could something like the code below work for you in this case? It’s a bit of ceremony, but this approach completely hides the implementation details from the module with the interface specification. The downside is that you _have_ to rely on polymorphism, but if you’re already using that it might no be such a big deal.

mytype.f90:

```fortran
module mytype_mod
    implicit none

    private
    public mytype_t
    public mytype_factory

    type, abstract :: mytype_t
        private
    contains
        procedure(public_sub), deferred :: public_sub
    end type

    interface
        subroutine public_sub(this)
            import mytype_t
            class(mytype_t), intent(inout) :: this
        end subroutine

        module function mytype_factory(i) result(this)
            integer, intent(in) :: i
            class(mytype_t), allocatable :: this
        end function
    end interface

end module

```

mytype\_impl.f90:

```fortran
submodule(mytype_mod) mytype_impl
    implicit none

    type, extends(mytype_t) :: mytype_impl_t
        integer :: i
    contains
        procedure :: public_sub => public_sub_impl
        procedure :: private_sub
    end type

contains

    module function mytype_factory(i) result(this)
        integer, intent(in) :: i
        class(mytype_t), allocatable :: this

        allocate(this, source=mytype_impl_t(i))
    end function

    subroutine public_sub_impl(this)
        class(mytype_impl_t), intent(inout) :: this

        write(*,*) 'This is public sub for mytype_impl_t with i = ', this%i
        call this%private_sub()
    end subroutine

    subroutine private_sub(this)
        class(mytype_impl_t), intent(inout) :: this

        write(*,*) 'This is private sub for mytype_impl_t with i = ', this%i
    end subroutine

end submodule

```

main.f90

```fortran
program main
    use mytype_mod, only: mytype_t, mytype_factory
    implicit none

    class(mytype_t), allocatable :: mytype

    mytype = mytype_factory(42)
    call mytype%public_sub()
end program

```

When run, it gives me the following output:

```auto
 This is public sub for mytype_impl_t with i = 42
 This is private sub for mytype_impl_t with i = 42

```

---

_[View the full topic](https://fortran-lang.discourse.group/t/compilation-time-vs-c/2430)._
