Topic
  • 2 replies
  • Latest Post - ‏2017-11-01T17:49:56Z by hemangh
hemangh
hemangh
2 Posts

Pinned topic Copying an extended derived type into its base (parent) type

‏2017-10-31T17:17:10Z | and base extended types

Is there a way to "painlessly" copy those elements of an extended type that are shared with its base type into a new base type array?

For example I want to copy a everything in the type (truck) :: A into a type(vehicle)::B, except for anual_milage. I don't want to go over each element of type A and pick and choose.

 

      module types
       implicit none

        type :: vehicle
           integer :: wheels
           character(len=6),allocatable,dimension(:)  :: service_date
        end type vehicle
        
        type,extends(vehicle) :: truck
          real, allocatable, dimension(:,:) :: anual_miles
        end type truck

        type track
          
          class(vehicle), pointer :: object => null()

        end type track


       end module

       program test_alloc
        use types
        implicit none

        
        type(truck)::A  
        type(vehicle) :: B

        A%wheels = 8
        allocate(A%service_date(2))
        A%service_date(1)='May16'
        A%service_date(2)='Sep16'
        allocate(A%anual_miles(10,10:17))
        A%anual_miles = 0.0
        A%anual_miles(1,10) = 3067.1
          
        call fill_x(B,A)        

       end program test_alloc


       subroutine fill_x(B,A)
        use types
        class(vehicle), target      :: A
        class(vehicle), allocatable :: B

!       allocate(B, mold=A)
!       what should go here?
        
       end subroutine

 

Attachments

Updated on 2017-10-31T17:18:40Z at 2017-10-31T17:18:40Z by hemangh
  • Rafik_Zurob
    Rafik_Zurob
    1 Post
    ACCEPTED ANSWER

    Re: Copying an extended derived type into its base (parent) type

    ‏2017-11-01T15:39:50Z  

    All extended types have a parent component that gives you access to the fields of the parent.  The parent component's name is the name of the base type.  So instead of calling fill_x, you can just have:

    B = A%vehicle
    
    Updated on 2017-11-01T15:40:32Z at 2017-11-01T15:40:32Z by Rafik_Zurob
  • Rafik_Zurob
    Rafik_Zurob
    1 Post

    Re: Copying an extended derived type into its base (parent) type

    ‏2017-11-01T15:39:50Z  

    All extended types have a parent component that gives you access to the fields of the parent.  The parent component's name is the name of the base type.  So instead of calling fill_x, you can just have:

    B = A%vehicle
    
    Updated on 2017-11-01T15:40:32Z at 2017-11-01T15:40:32Z by Rafik_Zurob
  • hemangh
    hemangh
    2 Posts

    Re: Copying an extended derived type into its base (parent) type

    ‏2017-11-01T17:49:56Z  

    All extended types have a parent component that gives you access to the fields of the parent.  The parent component's name is the name of the base type.  So instead of calling fill_x, you can just have:

    <pre dir="ltr">B = A%vehicle </pre>

    Thank you, that is what I ended up figuring out after a lot of head banging.

    allocate(B, source=A%vehicle) would do  the job of allocation and copying if B is type(vehicle)