title: How to call constructor of generic type in class constructor
body: <p>I want to create class <strong>Matrix2D</strong> with the following properties:</p>
  
  <ol>
  <li>Class should be generic</li>
  <li>Should be able to accept as many types as possible (ideally all)</li>
  <li>"Default" constructor should initialize all cells with default type values</li>
  <li>Correctly handle case, when type doesn't have default constructor (probably default parameter solves this)</li>
  </ol>
  
  <p>How I can do this?
  This is my sketch:</p>
  
  <pre><code>class Matrix2D&lt;T&gt; : Cloneable, Iterable&lt;T&gt; {
      private val array: Array&lt;Array&lt;T&gt;&gt;
      // Call default T() constructor if it exists
      // Have ability to pass another default value of type
      constructor(rows: Int, columns: Int, default: T = T()) {
          when {
              rows &lt; 1 -&gt; throw MatrixDimensionException("Number of rows should &gt;= 1")
              columns &lt; 1 -&gt; throw MatrixDimensionException("Number of columns should be &gt;= 1")
          }
          array = Array(rows, { Array(columns, { default }) })
      }
  }
  </code></pre>