title: Kotlin: How to define a variable whose type depends on the input?
body: <p>I have a function in Kotlin which takes a particular string as input. Depending on the input, I want to create a variable of a specific type and do some computations on it.</p>
  <p>For example,</p>
  <pre><code>fun compute(input: String): Any{
      if(input==&quot;2d&quot;){
          var point: Point2D;// Points2D - x: int, y: int
          //initilize and do some computations
          return point.findDistanceFromOrigin()
      }else if(input==&quot;2d-1&quot;){
          var point: Point2DWithP1AsOrigin;// Point2DWithP1AsOrigin - x: int, y: int
          //initilize and do some computations
          return point.findDistanceFromOrigin()
      }else if(input==&quot;2d-2&quot;){
          var point: Point2DWithP2AsOrigin;
          //initilize and do some computations
          return point.findDistanceFromOrigin()
      }
      .
      .
      .
  }
  </code></pre>
  <p>You can see in the above example, I want to initilize the type of point depending on the input and do computation and return.</p>
  <p>All the if-else conditions have the same code except for the definition of the variable. How can I put all this in a single block with something like this:</p>
  <pre><code>var point: if(input==&quot;2d) Point2D::class else if(input==&quot;2d-1&quot;) Point2DWithP1AsOrigin::class.....
  </code></pre>
  <p>How can I do that?</p>