 <p>If you need it more than once:</p> <pre><code>scala&gt; implicit class `nonempty or else`(val s: String) extends AnyVal { | def nonEmptyOrElse(other: =&gt; String) = if (s.isEmpty) other else s } defined class nonempty scala&gt; "abc" nonEmptyOrElse "def" res2: String = abc scala&gt; "" nonEmptyOrElse "def" res3: String = def </code></pre>