title: User-defined operators in Fortran
body: <p>I had a question about the correct way of programming user-defined operators in Fortran. To be more specific, I will provide the example of my problem. I am working on creating a user-defined data type for spherical particles called 'Particle'. I want to define an operator that takes an existing array of Particle objects and adds a new Particle object to it. I was wondering how I would go about defining user defined operators to do such an action.</p>
  
  <p>Currently I have, within the type definition for Particle, the following lines:</p>
  
  <pre><code>procedure, public:: addNewParticleTo
  generic:: operator(.spawn.) =&gt; addNewParticleTo
  </code></pre>
  
  <p>Following which, I have a subroutine that is defined as follows:</p>
  
  <pre><code>subroutine addNewParticleTo(a_LHS, a_RHS)
    implicit none
    class(Particle), dimension(:), allocatable, intent(in):: a_LHS
    class(Particle), dimension(:), allocatable, intent(inout):: a_RHS
    &lt;rest of the code&gt;
  end subroutine addNewParticleTo
  </code></pre>
  
  <p>I intend for the operator to be invoked as:</p>
  
  <pre><code>particle .spawn. particleArray
  </code></pre>
  
  <p>I was wondering if this is the correct way to go about doing this. Any suggestions or advise on this will be very helpful.</p>