[J3] Question on parent component naming

Vipul Parekh parekhvs at gmail.com
Tue Apr 9 19:55:22 UTC 2024


On Tue, Apr 9, 2024 at 11:26 AM Bader, Reinhold <Reinhold.Bader at lrz.de>
wrote:

> .. In a more complex type, this would be more complicated to handle,
>
> hence the use of a parent type structure constructor. The latter might
> also be overloaded, e.g. due to privatization of components.
>

Hello Reinhold,

Indeed the challenges in real life codes along the lines of complications
you have indicated can be considerable.

Please note a rather simple-minded, arguably quite naive, approach -one you
will surely know of - that does *scale well* in such situations involving
increasing complexity of derived types in actual codes is the judicious use
of "setter" methods bound to the derived type.  Any "overloaded" structure
constructor is then advised to religiously employ such setters e.g. below.
The callers - that is, the consumers of such types, then have the
flexibility of using the setters or the constructors. as suitable to their
circumstances e.g., the option (a) below.

module mod
   type t
      real, pointer :: p(:) => null()
   contains
      procedure setp
   end type
   generic :: t => construct_t
contains
   function construct_t( dat ) result(r)
   ! Constructor that employs setters bound to the type
      real, pointer, intent(in) :: dat(:)
      type(t) :: r
      call setp( r, dat )
   end function
   subroutine setp( this, dat )
   ! setter procedure
      class(t), intent(inout) :: this
      real, pointer, intent(in) :: dat(:)
      this%p => dat
   end subroutine
end module

program ref_parent
   use mod, my_t => t

   type, extends(my_t) :: td
     real :: data(3)
   end type

   type(td), target :: o_my_t

   ! o_my_t%my_t = my_t(o_my_t%data)  ! (x)
   ! o_my_t%t = my_t(o_my_t%data)     ! (y)
   !o_my_t%p => o_my_t%data           ! (z)
   call o_my_t%setp( o_my_t%data )    ! (a)

   block
      use, intrinsic :: iso_c_binding, only : c_loc, i8 => c_intptr_t
      print "(g0,z0)", "address in memory of o_my_t%p: ", transfer(
source=c_loc(o_my_t%p), mold=1_i8 )
      print "(g0,z0)", "address in memory of o_my_t%data: ", transfer(
source=c_loc(o_my_t%data), mold=1_i8 )
      print *, "is associated( pointer=o_my_t%p, target=o_my_t%data )? ", &
         associated( pointer=o_my_t%p, target=o_my_t%data ), "; expected is
t"
   end block

end program

A processor I tried helps build a program that works as I expect:
C:\temp>ifx /standard-semantics /free p.f -o p.exe
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version
2023.2.0 Build 20230627
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.36.32537.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\temp>p.exe
Address in memory of o_my_t%p: 7FF79C901048
Address in memory of o_my_t%data: 7FF79C901048
 Is associated( pointer=o_my_t%p, target=o_my_t%data )?  T ; expected is T

Best Regards,
Vipul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20240409/bdb7ec66/attachment.htm>


More information about the J3 mailing list