Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most of the answers here are proposing some variant of the "null object" pattern, by denoting an empty String to mean "undefined" (as in <code>val optId = if(id.isEmpty) None else Some(id)</code>)</p> <p>The catch here is that an empty string might be a valid value! This is true of <strong>any</strong> String, though you can mitigate the problem by using something really outrageous, possibly involving non-printable characters. e.g:</p> <pre><code>val UndefinedString = "THIS-IS-A-REALLY-UNLIKELY-VALID-VALUE" private def div( id: String = UndefinedString, cssClass: String = UndefinedString ): JQuery = { val optId = Option(id) filter (_ != UndefinedString ) val optCssClass = Option(cssClass) filter (_ != UndefinedString ) ... // deal with optId and optCssClass using the Scala-way™ ... } </code></pre> <p>Better still, you could use a different type to denote your null object. As you can't subclass <code>String</code> you'll have to bump your params up the type hierarchy and make them <code>CharSequence</code>s</p> <pre><code>object NullCharSeq extends CharSequence { def charAt(idx: Int): Char = ??? def length(): Int = 0 def subSequence(start: Int, end: Int): CharSequence = this def toString(): String = ??? } def charSeqToOptStr(cs: CharSequence): Option[String] = cs match { case NullCharSeq =&gt; None case x =&gt; Option(x) map (_.toString) } private def div( id: CharSequence = NullCharSeq, cssClass: CharSequence = NullCharSeq ): JQuery = { val optId = charSeqToOptStr(id) val optCssClass = charSeqToOptStr(cssClass) ... // deal with optId and optCssClass using the Scala-way™ ... } </code></pre> <p>It's a heavyweight pattern for one-shot usage, but the cost is quickly amortized if you use it a lot (<code>NullCharSeq</code> and <code>charSeqToOptStr</code> only need to be defined once in the codebase).</p> <p>There's also zero risk of mistakenly passing your "undefined" String as though it were a valid value. Plus, you gain the ability to directly accept CharBuffer/StringBuffer/StringBuilder as your arguments.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload