title: Assigning passive values or constants to a user-defined type
body: <p>So I'm working on an automatic differentiation toolbox in Fortran using operator overloading. I have previously implemented this in C++ but really need to get it to work in Fortran.</p>
  
  <p>I have the following module defined in Fortran:</p>
  
  <pre><code>     module adopov
           integer        :: indexcount
           integer, parameter :: tape_size = 1000
  !
  !....... ADtype
           public         :: ADtype
           type ADtype
              integer     :: index      = -1
              real        :: v          = 0.0
  !
           contains
              procedure        :: oo_asg
              generic, public  :: assignment(=) =&gt; oo_asg
           end type ADtype
  !
  !....... class tape
           public         :: ADtape
           type ADtape
              real        :: v          = 0.0
           end type ADtape
  !
  !....... interface(s)
           interface assignment(=)
              module procedure oo_asg
           end interface
  !
           type (ADtape), dimension(tape_size)  :: tape
  !
  !....... definitions
           contains
  !
  !....... assignment
           subroutine oo_asg (x,y)
              implicit none
              class(ADtype), intent(out)  :: x
              class(ADtype), intent(in)   :: y
  !
              tape(indexcount)%v          = y%v
              indexcount = indexcount + 1
              x%v     = y%v
              x%index = indexcount
           end subroutine oo_asg
  !
  end module adopov
  </code></pre>
  
  <p>In C++, I have a similar user-defined type as</p>
  
  <pre><code>class ADType {
      public:
        int index;
        double v;
        ADType() : index(-1), v(0) {};
        ADType(const double&amp;);
        ADType&amp; operator=(const ADType&amp;);
    };
  </code></pre>
  
  <p>where the constructor sets the initial values for the index and value parts. Next, I have a constructor for passive values or constants (of type double) so that I can define a new variable of class (<code>ADType</code>) whenever I have a double variable. For example, when I have:</p>
  
  <pre><code>ADType x;
  x = 2.0;
  </code></pre>
  
  <p>initially a new variable of type <code>ADType</code> is created with value set to 2.0, let's say <code>var1 = 2.0</code> and next (according to the assignment operator (=) defined in the class <code>ADType</code>) I will assign that variable to x, i.e. <code>x = var1</code>. This entire process is being recorded in a tape that counts operations and records the values and indices.</p>
  
  <p>Now, you may say "why do you have to do this?". Well, during the adjoint method of automatic differentiation using operator overloading, this is a necessary step.</p>
  
  <p>The way I do it in C++ is that I simply have the following two constructors:</p>
  
  <pre><code>ADType:: ADType(const double&amp; x): v(x) {
    tape[indexcounter].v = x;
    indexcounter++;
  };
  
  ADType&amp; ADType::operator=(const ADType&amp; x) {
    if (this==&amp;x) return *this;
    tape[indexcounter].v = v = x.v;
    indexcounter++;
    return *this;
  }
  </code></pre>
  
  <p>but I don't know how to implement the constructor for passive values and constants in Fortran.</p>