title: Use dynamic variables in a constructor
body: <p>I have always those costA-costE and similar variables in different parts in my code and I was interested how I could make this easier/faster with some "Groovy Magic":</p>
  
  <pre><code>def class Test{
  
      int costA
      int costB
      int costC
      int costD
      int costE
  
      Test(int A, int B, int C, int D, int E)
      {
          ['A'..'E'].flatten().each
          {
               (this."cost${it}"="${it}") //does not work as expected
          }
      }
  
  }
  
  ​Test test = new Test(1,2,3,4,5) ​​​​​​
  
  ['A'..'E'​​​​​​​​​].flatten().each​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​{​​println(test."cost${it}")}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
  </code></pre>
  
  <p>Results to:</p>
  
  <pre><code>65  // 'A' etc.
  66
  67
  68
  69
  70
  </code></pre>
  
  <p>The first part of it works, so something like this would be ok</p>
  
  <pre><code> this."cost${it}"=1 
  </code></pre>
  
  <p>But <code>"${it}"</code> just translates to A etc. instead of the value that is in A. <code>this."${it}"</code> would give me the wrong variable obviously, but how can I convince groovy to use the value that the constructor was given?</p>
  
  <p>(I tried <code>args."${it}"</code> or <code>args[0]</code> etc. but it didn't work)</p>
  
  <p>Bonusquestion (but not as important): Why do I need to flatten <code>['A'..'E']</code> to get a list with those 5 chars? Aka would there be a cleaner/shorter way to get the same list?</p>