Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is difficult to do cleanly in Fortran 90. It is straight forward in Fortran 2003.</p> <p>Resolution of the procedure to call when the generic reference <code>fun</code> is encountered (in <code>CALL fun(pt)</code>) is done at compile time, based on the declared type of <code>pt</code>.</p> <p>If you are limited to Fortran 90, then effectively you will need to maintain a flag of some sort that indicates at runtime which particular derived type you want to work with, have a named object for each of type A and type B, and everytime you want to reference <code>fun</code> have an IF construct that selects the correctly named argument. </p> <p>(If the size of the objects is significant you can arrange for them to have common storage.)</p> <p>Something like:</p> <pre><code>TYPE(A) :: pt_A TYPE(B) :: pt_B ... IF (input .EQ. 'A') THEN CALL fun(pt_A) ELSE CALL fun(pt_B) END IF </code></pre> <p>In F2003, you would define a common parent type, that had a specific binding named fun. <code>pt</code> would then be a polymorphic allocatable object, allocated based on input to either type A or type B as appropriate.</p> <pre><code>TYPE :: Parent CONTAINS PROCEDURE(parent_Fun), DEFERRED :: Fun END TYPE Parent ABSTRACT INTERFACE SUBROUTINE parent_Fun(obj) IMPORT :: Parent IMPLICIT NONE CLASS(Parent), INTENT(IN) :: obj END SUBROUTINE parent_Fun END INTERFACE TYPE, EXTENDS(Parent) :: A REAL :: x, y CONTAINS PROCEDURE :: A =&gt; A_Fun END TYPE A TYPE, EXTENDS(Parent) :: B INTEGER :: x, y CONTAINS PROCEDURE :: B =&gt; B_Fun END TYPE B CLASS(Parent), ALLOCATABLE :: pt ... IF (input .EQ. 'A') THEN ALLOCATE(A:: pt) ELSE ALLOCATE(B:: pt) END IF ... CALL pt%Fun() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload