title: Kotlin Enums: function that refers to whatever Enum Object it's called from
body: <p>I am trying to write an enum function <code>print()</code> that can be called as follows, from an object in the enum <code>Molecule</code>:</p>
  
  <pre><code>Molecule.HYDROGEN_MOLECULE.print()
  myMolecule.print()
  </code></pre>
  
  <p>I don't want to have to feed the function a specific Enum Object:</p>
  
  <pre><code>Molecule.print(Molecule.HYDROGEN)
  </code></pre>
  
  <p>However, when I create the function in <code>Molecule</code>, I get an error when I try to reference the List of <code>MolecularFragment</code>. </p>
  
  <p>What syntax I need to use in  print() to make something like <code>myMolecule.print()</code> reference the list of <code>MolecularFragment</code> for that particular enum object?</p>
  
  <pre><code>enum class Molecule (val MolecularFragments : List&lt;MolecularFragment&gt; = emptyList()) {
  
      HYDROGEN_MOLECULE(listOf(MolecularFragment.H2)),
  
      WATER_MOLECULE(listOf(MolecularFragment.H2O)),
      // ...
      HEXANE_MOLECULE(listOf(MolecularFragment.CH3, MolecularFragment.CH2));
  
      fun print() {
  
          **var x : List&lt;MolecularFragment&gt; = Molecule.MolecularFragments**
          x.forEach() {
              println("$it")
          }
          println(" --- ${Molecule.toString()}")
      }
  }
  </code></pre>