Concatenating optional strings in Scala I have two optional strings, any of them can be None. I want to create their combination with a delimiter between them if they both exist.
I expect to be able to come with a more concise and nicer solution.
val seq = (Some(&quot;abc&quot;), None)
val none = (None, None)
val both = (Some(&quot;abc&quot;), Some(&quot;x&quot;))

def concat(s: (Option[String], Option[String])): Option[String] = {
  s match {
    case (Some(a), Some(b)) =&gt; Some(a + &quot;, &quot; + b)
    case (a, None)          =&gt; a
    case (None, b)          =&gt; b
  }
}

concat(seq)
concat(none)
concat(both)

Can this be rewritten somehow in a more elegant way? The input can be changed from Tuple to Seq if desired.
As you seem to have already noticed yourself, the fact that the input to the function is a pair of strings is somewhat awkward. It violates the Zero-One-Infinity Rule.
You might be worried about YAGNI or the Rule Of Three, but I would argue that it is okay to fudge those guidelines here a little bit, since

the extension from two to many is obvious,
simplifies the code, and
is in service to another guideline.

One main question you need to ask yourself, which I feel is not really answered in the specification you gave in your question nor in the code snippet you are asking to have reviewed, relates to this statement from your question [bold emphasis mine]:

I want to create their combination with a delimiter between them if they both exist.

What, precisely do you mean by &quot;if they both exist&quot;? If we are looking at ways that an Option[String] in Scala can exist or maybe not exist, I can think of many different scenarios:

It can be null. (Unfortunately, since Scala is designed to tightly integrate with the underlying platform, and all of the &quot;interesting&quot; underlying platforms all have a concept of null, it is hard to get rid of this.)
It can be None.
It can be Some(&quot;&quot;), i.e. the empty string.
It can be Some(&quot;  &quot;), i.e. a string consisting purely of U+0020 Space characters.
It can be Some(&quot;  &quot;), i.e. a string consisting purely of whitespace characters other than U+0020 Space. (There are at least 16 different whitespace characters that are not U+0020 which I have found in about 30s of research.)
It can be Some(&quot;​&quot;), i.e. a string consisting purely of zero-width characters. (Note, this is not an empty string! It contains a U+200B Zero-width space. For added fun, this particular character is not considered Whitespace by the Unicode standard!)

And these are just some of the things I can think of off the top of my head.
For example, both your original code in the question and your revised code in the answer will return Some(&quot;, &quot;) when passed two empty strings, or Some(&quot; ,  &quot;) when passed two strings consisting of one space character.
If you are willing to accept a very broad definition, then a very simple implementation could be something like this:
def concat(s: Option[String]*) = s.flatten.mkString(&quot;, &quot;)

However, this will always give you a String, it will not give you an Option[String]. For your None, None example, it will give you the empty string:
concat(None, None)
//=&gt; &quot;&quot;

[Scastie link]
Any specification more complex than this very simple one, including the one from your question, will complicate the code, there is not much you can do about that. There is no getting around the fact that text processing looks deceptively simple (everybody knows what an &quot;empty string&quot; or a &quot;character&quot; is, right?) but is actually very complex.


&lt;script src="https://scastie.scala-lang.org/JoergWMittag/1EScnd33SZ68MEXCCXjTQg.js"&gt;&lt;/script&gt;