[J3] [EXTERNAL] Re: Overriding type-bound procedure used in generic

Reuben D. Budiardja reubendb at ornl.gov
Tue May 6 21:39:08 UTC 2025


Thanks Malcolm. I suspected that this was the case but it's good to have 
a clarification.

I think there's a (separate) compelling use case to have a type-bound 
procedure accessible only to the derived-type and its extensions, and 
therefore overridable by the extension (defined in a different module).
The code I have is also trying to achieve this with PRIVATE type-bound 
procedure, and as I see now, mistakenly, since it is not what the 
standard support.

Best,
Reuben

On 5/5/25 21:19, Malcolm Cohen via J3 wrote:
> 
> Hi Reuben,
> 
> 1. The output should be
> 
> Init F
> Init F
> 
> This is because the INIT_T in module F_M is PRIVATE, it is thus invisible outside the module, and therefore cannot be overwritten outside the module. It is permitted (and normal) to be able to have components in a type extension outside the module with the same name as private components inside the module. It is analogous to
> 
> MODULE M1
> REAL,PRIVATE :: X
> END
> MODULE M2
> USE M1
> ASYNCHRONOUS X
> END
> 
> There is no visibility of X in M1 from M2, thus the declaration in M2 is valid and declares a new thing (with implicit typing), it does not make X from M1 an asynchronous variable while in M2.
> 
> Similarly, the INIT_T in G_T in G_M is a new type-bound procedure, it does not reuse the slot in the dispatch table for the INIT_T in F_T.
> 
> 2. Making INIT_T PUBLIC will cause the type extension to override the INIT_T from F_M, thus producing the expected results.
> 
> Cheers,
> -- 
> ..............Malcolm Cohen, NAG Oxford/Tokyo.
> 
> -----Original Message-----
> From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Reuben D. Budiardja via J3
> Sent: Tuesday, May 6, 2025 1:47 AM
> To: General J3 interest list <j3 at mailman.j3-fortran.org>
> Cc: Reuben D. Budiardja <reubendb at ornl.gov>
> Subject: [J3] Overriding type-bound procedure used in generic
> 
> Hi all,
> 
> I'd like some help with this.
> 
> Consider the code below.
> 
> 1. What should the output be?
> 
> Two compilers I tried give:
> Init G
> Init F
> 
> while two other compilers give:
> Init F
> Init F
> 
> 2. Should it matter if the access spec of the type-bound statement "Init_T" is changed to "public"? My understanding is that it should not (from 19.5.4 24-007), but in my test if I do that all four compilers then agree in the output:
> Init_G
> Init_F
> 
> 
> Thanks,
> Reuben
> 
> -----------------------------------
> 
> module F_M
>     implicit none
>     private
> 
>     type, public :: F_T
>     contains
>       procedure, private, pass :: &
>         Init_T
>       generic, public :: &
>         Init => Init_T
>     end type F_T
> contains
>     subroutine Init_T ( FS )
>       class ( F_T ), intent ( inout ) :: &
>         FS
>       print*, 'Init F'
>     end subroutine Init_T
> 
> end module F_M
> 
> 
> module G_M
>     use F_M
>     implicit none
>     private
> 
>     type, public, extends ( F_T ) :: G_T
>     contains
>       procedure, private, pass :: &
>         Init_T
>     end type G_T
> contains
>     subroutine Init_T ( FS )
>       class ( G_T ), intent ( inout ) :: &
>         FS
>       print*, 'Init G'
>     end subroutine Init_T
> 
> end module G_M
> 
> 
> program Test
> 
>     use G_M
>     implicit none
>     type ( G_T ), allocatable :: &
>       G
> 
>     allocate ( G )
>     call G % Init (  )
>     call G % F_T % Init ( )
> 
> end program Test
> 
> 



More information about the J3 mailing list