title: Purpose of AsyncTask varargs parameters
body: <p>What are the reasons that Google use <code>varargs</code> for the parameters in the <a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="noreferrer"><code>AsyncTask</code></a>? For example the methods <a href="http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29" rel="noreferrer"><code>execute()</code></a>, <a href="http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground%28Params...%29" rel="noreferrer"><code>doInBackground()</code></a> and <a href="http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress%28Progress...%29" rel="noreferrer"><code>publishProgress()</code></a> all use the <code>[Type]...</code> notation. </p>
  
  <p>I think that makes it "harder" to use so they must have some good reasons which I overlooked?</p>
  
  <hr>
  
  <p>So, either we have no parameters, one or many parameters. Let's break it down: </p>
  
  <ol>
  <li><p><strong>No parameters</strong> (easy): <code>Params</code> parameter is <code>Void</code> and that's it. (The methods cannot use it... so that's pretty safe.)</p></li>
  <li><p><strong>One parameter</strong>: Here, I at least, feel the need to make a check at the beginning of the <code>doInBackground()</code> method. For example, here is a task receiving an <code>Integer</code> and producing a result of type <code>Double</code>:</p>
  
  <pre><code>public Double doInBackground(Integer... myParameters) {
      // we are only expecting one parameter
      if (myParameters.length != 1)
          throw new IllegalArgumentException("!= 1");
  
      return 100d * myParameters[0];
  }
  </code></pre></li>
  <li><p><strong>More than one parameter</strong>. Now here must be where Google made the right choice? But as I see it's either you are interested in a list of parameters of the <em>same type</em>, or you want <em>different types</em> of parameters. Google only addressed one of these cases (with different types you need some kind of common interface. In many cases I end up with <code>Object...</code> and that isn't really type safe...)</p></li>
  </ol>
  
  <hr>
  
  <p>So, what is the problem if we just remove the <code>varargs</code> altogether? Here's a subset of the methods:</p>
  
  <pre><code>class AsyncTask&lt;Param, Progress, Result&gt; {
  
      abstract Result doInBackground(Param param);
      void publishProgress(Progress progress) { ... }
  }
  </code></pre>
  
  <p>This would work for all the cases above. For example, if we want to handle an array of parameters we could just use an array type <code>param</code>:</p>
  
  <pre><code>class MyAsyncTask extends AsyncTask&lt;String[], Integer, String&gt; { 
  
      String doInBackground(String[] param) {
          return Arrays.toString(param);
      }
  }
  </code></pre>
  
  <p>I don't see when it could be of any practical use. But I'm sure I'm missing something curia that I need to know about. :)</p>