title: null to listOf(), not null to listOf(value) in Kotlin in one liner?
body: <p>Let <code>f()</code> return a nullable value.</p>
  
  <p>What I want to do is that<br>
  if <code>f()</code> is null, get an empty list,<br>
  else if <code>f()</code> is not null, get a list of the single item <code>value</code>.</p>
  
  <p>In Scala, we can do something like this:</p>
  
  <pre><code>Option(f()).toList
  </code></pre>
  
  <p>or more verbosely</p>
  
  <pre><code>Option(f()).map(v =&gt; List(v)).getOrElse(List.empty)
  </code></pre>
  
  <p>In Kotlin, there is no <code>Option</code> (assuming no Funktionale library), and null does not have <code>toList()</code> unlike (<code>None: Option</code>) in Scala.</p>
  
  <p>We have the Elvis operator, but null will be inside the <code>listOf()</code> function, so it will be</p>
  
  <pre><code>listOf(f() ?: /* What can I do here? */)
  </code></pre>
  
  <p>What we want for null is <code>listOf(/*no argument */)</code>, but the Elvis operator requires an argument, so <code>listOf(f() ?: )</code> will result in a compile error.</p>
  
  <p>At least we can do</p>
  
  <pre><code>val v = f()
  if (v == null) listOf() else listOf(v)
  </code></pre>
  
  <p>but it is a two liner.</p>
  
  <p>Is there some expression for this?</p>
  
  <p>Where I will use this expression is in the class's primary constructor default argument, so if it is not a one liner, it will be enclosed in brackets, so something like this:</p>
  
  <pre><code>class A(
      val p1: List&lt;V&gt; = run {
          val v = f()
          if (v == null) listOf() else listOf(v)
      },
      val p2: ... = ...,
      ...)
  </code></pre>
  
  <p>This looks pretty ugly, isn't it?</p>