title: Passing null value to Kotlin method with non nullable default arguments
body: <p>I am new to Kotlin and I found one of the features which Kotlin differs from Java is method default arguments.
  <a href="https://kotlinlang.org/docs/functions.html#default-arguments" rel="nofollow noreferrer">https://kotlinlang.org/docs/functions.html#default-arguments</a></p>
  <p>I am creating a simple Kotlin application which passes null values to a method with 2 non nullable default arguments, which results in a compilation error - <strong>Type mismatch: inferred type is String? but String was expected)</strong>.</p>
  <pre><code>fun greet(firstName: String = &quot;Hello&quot;, lastName: String = &quot;world&quot;) {
      println(&quot;${firstName} ${lastName}&quot;)
  }
  
  fun main(args: Array&lt;String&gt;) {
      greet(args.getOrElse(0) { null }, args.getOrElse(1) { null })
  }
  </code></pre>
  <p>One workaround is to change the method arguments to nullable, but the cleaniness of the original method is lost.</p>
  <pre><code>fun greet(firstName: String?, lastName: String?) {
      val firstName2 = firstName ?: &quot;Hello&quot;
      val lastName2 = lastName ?: &quot;world&quot;
      println(&quot;${firstName2} ${lastName2}&quot;)
  }
  
  fun main(args: Array&lt;String&gt;) {
      greet(args.getOrElse(0) { null }, args.getOrElse(1) { null })
  }
  </code></pre>
  <p>If I have to keep the original method signature, I can think of below alternative solutions.</p>
  <p><strong>Duplicate the default argument definitions in method signature and method caller</strong></p>
  <pre><code>fun greet(firstName: String = &quot;Hello&quot;, lastName: String = &quot;world&quot;) {
      println(&quot;${firstName} ${lastName}&quot;)
  }
  
  fun main(args: Array&lt;String&gt;) {
      greet(args.getOrElse(0) { &quot;Hello&quot; }, args.getOrElse(1) { &quot;world&quot; })
  }
  </code></pre>
  <p><strong>Separate method calls based on the number of arguments</strong></p>
  <pre><code>fun greet(firstName: String = &quot;Hello&quot;, lastName: String = &quot;world&quot;) {
      println(&quot;${firstName} ${lastName}&quot;)
  }
  
  fun main(args: Array&lt;String&gt;) {
      when (args.size) {
          0 -&gt; greet()
          1 -&gt; greet(args[0])
          2 -&gt; greet(args[0], args[1])
      }
  }
  </code></pre>
  <p>All 3 solutions print <code>Hello world</code> when no argument is supplied. I am wondering if there are other better alternative solutions.</p>